server/browser_npc_outfits_test.go (5921B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "context" 6 "crypto/sha256" 7 "encoding/json" 8 "fmt" 9 "math" 10 "net/http" 11 "net/http/httptest" 12 "os" 13 "os/exec" 14 "path/filepath" 15 "runtime" 16 "testing" 17 "time" 18 ) 19 20 func TestBrowserNPCRecoveredAppearances(t *testing.T) { 21 testBrowserNPCAppearances(t, []string{ 22 "705/district/0/fixed/15", "710/district/0/fixed/0", 23 "705/district/0/fixed/9", "705/district/0/fixed/13", 24 "127/district/0/fixed/1", 25 }) 26 } 27 28 func TestBrowserNPCOutfits(t *testing.T) { 29 testBrowserNPCAppearances(t, []string{"710/district/0/fixed/8", "710/district/1/fixed/2", "710/district/0/fixed/18", "710/district/0/fixed/19", 30 "950/district/1/fixed/0", "952/district/3/fixed/0", "954/district/1/fixed/0", 31 "950/district/2/fixed/0", "950/district/2/fixed/1", "952/district/0/fixed/0", "952/district/0/fixed/1", 32 "954/district/2/fixed/0", "954/district/2/fixed/1", 33 "950/district/1/group/0/authored_point", "950/district/3/group/0/authored_point", "950/district/4/group/0/authored_point", 34 "952/district/2/group/0/authored_point", "952/district/3/group/1/authored_point", "954/district/4/group/3/spread_authored_point"}) 35 } 36 37 func TestBrowserNPCBestEffortOutfits(t *testing.T) { 38 testBrowserNPCAppearances(t, []string{ 39 "705/district/0/fixed/10", "710/district/0/fixed/4", 40 "705/district/0/fixed/16", "705/district/0/fixed/1", 41 "3100/district/1/fixed/0", "716/district/7/group/1/authored_point", 42 }) 43 } 44 45 func TestBrowserNPCCreatureAppearances(t *testing.T) { 46 // One original placement per distinct creature body in the three training routes. 47 testBrowserNPCAppearances(t, []string{ 48 "950/district/1/group/1/authored_point", "950/district/1/group/2/authored_point", "950/district/2/group/1/authored_point", 49 "950/district/4/group/1/authored_point", "950/district/4/group/3/authored_point", "950/district/6/fixed/0", 50 "952/district/0/group/1/authored_point", "952/district/2/group/2/authored_point", "952/district/3/group/0/authored_point", "952/district/5/fixed/0", 51 "954/district/0/group/0/authored_point", "954/district/0/group/1/authored_point", "954/district/0/group/2/spread_authored_point", 52 "954/district/0/group/3/spread_authored_point", "954/district/5/fixed/0", 53 }) 54 } 55 56 func testBrowserNPCAppearances(t *testing.T, routes []string) { 57 t.Helper() 58 if os.Getenv("AO_GAME_TEST") != "1" { 59 t.Skip("set AO_GAME_TEST=1 for two-client NPC appearances") 60 } 61 _, source, _, _ := runtime.Caller(0) 62 root := filepath.Dir(filepath.Dir(source)) 63 output := filepath.Join(root, "local/npc-appearances", time.Now().Format("20060102-150405")) 64 for _, route := range routes { 65 t.Run(route, func(t *testing.T) { 66 world := newTestServer(t) 67 var npc *character 68 for _, actor := range world.characters { 69 if actor.npcDefinitionID == route && (npc == nil || actor.id < npc.id) { 70 npc = actor 71 } 72 } 73 if npc == nil { 74 t.Fatal("appearance actor is absent from the original population") 75 } 76 // Keep the appearance target at its authored post through page replay. 77 if npc.motion != nil { 78 npc.motion.roaming = false 79 } 80 // Isolate recovered profiles at their original posts so nearby actors cannot obscure them. 81 if gamedata.NPCGroups[route].Visual.Head != 0 { 82 for id := range world.characters { 83 if id != npc.id { 84 delete(world.characters, id) 85 } 86 } 87 } 88 if npc.appearance>>4&7 < 6 && len(npc.inventory.Items) == 0 && gamedata.NPCGroups[route].Visual.Head == 0 { 89 t.Fatal("human appearance actor has no outfit") 90 } 91 // Frame large creatures using their scene collision extent. 92 npc.geometry = world.geometry 93 body := npc.collisionBody() 94 distance := max(float32(3), 2*(body.offset[1]+body.radius)) 95 // These tall render models extend beyond their collision sphere. 96 switch npc.monsterData { 97 case 30252: 98 distance = 8 99 case 17708: 100 distance = 12 101 } 102 for index, offset := range []float32{-1, 1} { 103 player := world.newCharacter() 104 player.playfield, player.position = npc.playfield, npc.position 105 // Stand in front of the authored facing, with room for both observers. 106 yaw := 2 * math.Atan2(float64(npc.rotation[1]), float64(npc.rotation[3])) 107 dx := float32(math.Sin(yaw))*distance + float32(math.Cos(yaw))*offset 108 dz := float32(math.Cos(yaw))*distance - float32(math.Sin(yaw))*offset 109 player.position[0] += dx 110 player.position[1] += .75 111 player.position[2] += dz 112 yaw = math.Atan2(float64(-dx), float64(-dz)) 113 player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))} 114 player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("npc-outfit-%d", index)))) 115 if err := world.saveUser(player); err != nil { 116 t.Fatal(err) 117 } 118 } 119 npcID := npc.id 120 startTestService(t, world) 121 mux := http.NewServeMux() 122 mux.Handle("/", world.httpHandler()) 123 mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) { 124 npc := world.characters[npcID] 125 armor := [8]int32{} 126 for index := range armor { 127 armor[index] = npc.equipmentBonus(90 + uint32(index)) 128 } 129 json.NewEncoder(w).Encode(map[string]any{"id": npc.id, "route": route, "name": npc.name, "monster": npc.monsterData, "head": npc.headMesh, 130 "items": npc.inventory.Items, "armor": armor, "position": npc.position, "rotation": npc.rotation, "visual": gamedata.NPCGroups[route].Visual}) 131 })) 132 host := httptest.NewServer(mux) 133 t.Cleanup(host.Close) 134 ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) 135 defer cancel() 136 command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_npc_outfits.mjs"), host.URL, filepath.Join(output, route)) 137 command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr 138 err := command.Run() 139 t.Logf("Browser artifacts: %s", filepath.Join(output, route)) 140 if err != nil { 141 t.Fatal(err) 142 } 143 }) 144 } 145 }