aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/npc_facing_test.go (3315B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"math"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestFixedNPCFacingUsesOriginalDegrees(t *testing.T) {
     12 	data := testNPCData()
     13 	data.Playfields[0].Districts[0].Groups = []gamedata.NPCGroup{
     14 		*gamedata.NPCGroups["705/district/0/fixed/22"],
     15 		*gamedata.NPCGroups["705/district/0/fixed/2"],
     16 		*gamedata.NPCGroups["705/district/0/fixed/0"],
     17 		*gamedata.NPCGroups["705/district/0/fixed/1"],
     18 		*gamedata.NPCGroups["705/district/0/group/0/authored_point"],
     19 	}
     20 	useNPCData(t, data)
     21 
     22 	spawns, err := readNPCSpawns()
     23 	if err != nil {
     24 		t.Fatal(err)
     25 	}
     26 
     27 	want := [][4]float32{
     28 		{0, 0.8870108, 0, 0.4617486},
     29 		{0, 0.70710677, 0, 0.70710677},
     30 		{0, 1, 0, 0},
     31 		{0, 0, 0, 1},
     32 		{0, 0, 0, 1},
     33 	}
     34 	if len(spawns) != len(want) {
     35 		t.Fatal("fixed facing changed population count", len(spawns))
     36 	}
     37 	for index := range spawns {
     38 		packet := characterPacket(spawns[index])
     39 		for component, value := range want[index] {
     40 			wire := math.Float32frombits(binary.BigEndian.Uint32(packet[51+component*4:]))
     41 			if math.Abs(float64(spawns[index].rotation[component]-value)) > 1e-6 || wire != spawns[index].rotation[component] {
     42 				t.Fatalf("spawn %s rotation %v, wire component %d=%g", spawns[index].npcDefinitionID, spawns[index].rotation, component, wire)
     43 			}
     44 		}
     45 	}
     46 }
     47 
     48 func TestFixedNPCRespawnRestoresAuthoredFacingAndReloadPreservesIt(t *testing.T) {
     49 	world, _ := newRespawnWorld(t)
     50 	gamedata.NPCGroups["test/npc"].FixedSource = &gamedata.NPCFixedSpawnSource{Angles: [2]int16{90, 30}}
     51 	world.characters[1000000].rotation = [4]float32{0, -1, 0, 0}
     52 
     53 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
     54 	killed := world.characters[1].nextAttack
     55 	world.advanceCombat(killed)
     56 	world.advanceNPCRespawns(killed.Add(100 * time.Second))
     57 	cold := newTestInventoryServer(t)
     58 
     59 	for _, current := range []*server{world, cold} {
     60 		npc := current.characters[1000001]
     61 		if npc == nil || npc.rotation != [4]float32{0, 0.70710677, 0, 0.70710677} {
     62 			t.Fatalf("replacement lost authored facing: %+v", npc)
     63 		}
     64 	}
     65 }
     66 
     67 func TestLivingNPCReloadRestoresAuthoredFacing(t *testing.T) {
     68 	for _, scenario := range []struct {
     69 		name       string
     70 		placement  string
     71 		appearance string
     72 	}{
     73 		{"stationary", "fixed_point", "placeholder_creature"},
     74 		{"roaming", "authored_point", "placeholder_creature"},
     75 		{"combat", "fixed_point", "inferred_species"},
     76 	} {
     77 		t.Run(scenario.name, func(t *testing.T) {
     78 			data := testNPCData()
     79 			data.Playfields[0].Districts[0].Groups[0].FixedSource = &gamedata.NPCFixedSpawnSource{Angles: [2]int16{90, 30}}
     80 			data.Playfields[0].Districts[0].Groups[0].PlacementStatus = scenario.placement
     81 			data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = scenario.appearance
     82 			data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{1, 1}
     83 			useNPCData(t, data)
     84 			world := newTestServer(t)
     85 			world.characters[1000000].rotation = [4]float32{0, 0, 0, 1}
     86 			if world.characters[1000000].motion != nil {
     87 				world.characters[1000000].motion.moving = true
     88 			}
     89 			if err := world.flushWorld(); err != nil {
     90 				t.Fatal(err)
     91 			}
     92 
     93 			cold := newTestInventoryServer(t)
     94 
     95 			if cold.characters[1000000].rotation != [4]float32{0, 0.70710677, 0, 0.70710677} {
     96 				t.Fatalf("%s actor loaded rotation %v", scenario.name, cold.characters[1000000].rotation)
     97 			}
     98 		})
     99 	}
    100 }