server/npc_outfits_test.go (4866B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "net" 7 "reflect" 8 "testing" 9 "time" 10 ) 11 12 func TestNPCEliteOutfitSurvivesReload(t *testing.T) { 13 data := testNPCData() 14 data.Playfields[0].Districts[0].Groups[0] = *gamedata.NPCGroups["710/district/0/fixed/8"] 15 useNPCData(t, data) 16 world := newTestServer(t) 17 18 if err := world.flushWorld(); err != nil { 19 t.Fatal(err) 20 } 21 cold := newTestInventoryServer(t) 22 23 want := []ownedItem{ 24 {Type: 51022, Template: 22250, Quality: 200, Slot: 18, Count: 1}, 25 {Type: 51022, Template: 22068, Quality: 200, Slot: 21, Count: 1}, 26 {Type: 51022, Template: 21959, Quality: 200, Slot: 23, Count: 1}, 27 {Type: 51022, Template: 22200, Quality: 200, Slot: 24, Count: 1}, 28 {Type: 51022, Template: 21959, Quality: 200, Slot: 25, Count: 1}, 29 {Type: 51022, Template: 22335, Quality: 200, Slot: 27, Count: 1}, 30 {Type: 51022, Template: 22146, Quality: 200, Slot: 30, Count: 1}, 31 } 32 for _, current := range []*server{world, cold} { 33 npc := current.characters[1000000] 34 if !reflect.DeepEqual(npc.inventory.Items, want) || npc.headMesh != 40629 { 35 t.Fatalf("guard outfit absent or changed after reload: head=%d items=%+v", npc.headMesh, npc.inventory.Items) 36 } 37 if binary.BigEndian.Uint32(characterPacket(npc)[37:41])&8 == 0 { 38 t.Fatal("human NPC spawn disables native clothing changes") 39 } 40 41 // Observe the actual login sequence, including after a cold world load. 42 player := current.newCharacter() 43 player.playfield, player.position = npc.playfield, npc.position 44 shutdown := startTestService(t, current) 45 client, connection := net.Pipe() 46 defer client.Close() 47 client.SetDeadline(time.Now().Add(5 * time.Second)) 48 go func() { defer connection.Close(); current.serveWorldConnection(connection) }() 49 client.Write(systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})) 50 var packets [][]byte 51 for _, packet := range readWorldUpdates(t, client) { 52 if binary.BigEndian.Uint32(packet[28:32]) == npc.id { 53 packets = append(packets, packet) 54 } 55 } 56 if len(packets) < 8 || binary.BigEndian.Uint32(packets[0][20:24]) != 0x271b3a6b { 57 t.Fatalf("missing outfit spawn sequence: %x", packets) 58 } 59 nameEnd := 70 + int(packets[0][69]) 60 if binary.BigEndian.Uint32(packets[0][37:41])&0x40003 != 0x40002 || 61 binary.BigEndian.Uint32(packets[0][nameEnd+29:nameEnd+33]) != 26090 { 62 t.Fatal("outfitted NPC must retain IsNpc and MonsterData using humanoid initialization") 63 } 64 for index, item := range want { 65 packet := packets[index+1] 66 if binary.BigEndian.Uint32(packet[20:24]) != 0x35505644 || 67 binary.BigEndian.Uint32(packet[36:40]) != item.Template || binary.BigEndian.Uint32(packet[44:48]) != 200 || 68 binary.BigEndian.Uint32(packet[52:56]) != 6 || binary.BigEndian.Uint32(packet[60:64]) != item.Slot { 69 t.Fatalf("outfit action %d: %x", index, packet) 70 } 71 } 72 client.Close() 73 var items int 74 if err := current.db.QueryRow("SELECT count(*) FROM items").Scan(&items); err != nil || items != 0 { 75 t.Fatalf("definition equipment allocated transferable player items: %d %v", items, err) 76 } 77 if err := shutdown(); err != nil { 78 t.Fatal(err) 79 } 80 } 81 } 82 83 func TestCivilianRoleOutfitsSurviveReloadWithoutCombat(t *testing.T) { 84 for _, test := range []struct { 85 definition string 86 head uint32 87 templates [5]uint32 88 }{ 89 {"705/district/0/fixed/1", 40694, [5]uint32{82912, 82919, 82919, 82911, 82913}}, 90 {"3100/district/1/fixed/0", 40635, [5]uint32{82102, 82092, 82092, 82101, 82100}}, 91 {"716/district/7/group/1/authored_point", 40252, [5]uint32{82102, 82092, 82092, 82101, 82100}}, 92 } { 93 t.Run(test.definition, func(t *testing.T) { 94 data := testNPCData() 95 data.Playfields[0].Districts[0].Groups[0] = *gamedata.NPCGroups[test.definition] 96 useNPCData(t, data) 97 world := newTestServer(t) 98 99 if err := world.flushWorld(); err != nil { 100 t.Fatal(err) 101 } 102 cold := newTestInventoryServer(t) 103 104 for _, current := range []*server{world, cold} { 105 npc := current.characters[1000000] 106 if len(npc.inventory.Items) != 5 || npc.headMesh != test.head || npc.combatProfile != nil || npc.attackableNPC() { 107 t.Fatal("civilian outfit or combat eligibility changed", npc.inventory.Items, npc.headMesh) 108 } 109 for index, slot := range []uint32{21, 23, 25, 27, 30} { 110 want := ownedItem{Type: 51022, Template: test.templates[index], Quality: 1, Slot: slot, Count: 1} 111 if npc.inventory.Items[index] != want { 112 t.Fatal("civilian garment changed", npc.inventory.Items[index]) 113 } 114 } 115 for stat := uint32(90); stat <= 97; stat++ { 116 if npc.equipmentBonus(stat) != 0 { 117 t.Fatal("civilian clothing added armor", stat) 118 } 119 } 120 var items int 121 if err := current.db.QueryRow("SELECT count(*) FROM items").Scan(&items); err != nil || items != 0 { 122 t.Fatal("civilian outfit allocated transferable items", items, err) 123 } 124 } 125 }) 126 } 127 }