server/doors_test.go (4635B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "encoding/hex" 7 "fmt" 8 "testing" 9 ) 10 11 func TestDoorFullUpdateKeepsNativePlacementAndInheritance(t *testing.T) { 12 door := worldDoor{id: 8000001, template: 41565, position: [3]float32{15, 0, 29.999}, rotation: [4]float32{0, 1, 0, 0}} 13 14 packet, err := doorPacket(9000001, door) 15 if err != nil { 16 t.Fatal(err) 17 } 18 packet = targetPacket(packet) 19 packet = packet[:binary.BigEndian.Uint16(packet[6:8])] 20 21 if len(packet) != 135 || binary.BigEndian.Uint32(packet[16:20]) != 0x365a5071 || 22 binary.BigEndian.Uint32(packet[20:24]) != 51016 || binary.BigEndian.Uint32(packet[24:28]) != 8000001 { 23 t.Fatal("door identity or native message header changed") 24 } 25 if got := hex.EncodeToString(packet[29:]); got != "0000000b0000000000000000417000000000000041effdf4000000003f8000000000000000000000008954410000000000000000000000000fc4000000170000a25d000002bd000000010000000080081401000000000000000200000000000003f10000000200000000" { 26 t.Fatal("native door placement, state or subclass tail changed", got) 27 } 28 } 29 30 func TestDoorFullUpdateKeepsIndependentLockAndOpenState(t *testing.T) { 31 for _, scenario := range []struct { 32 locked, open bool 33 flags uint32 34 }{ 35 {false, false, 0x80081401}, 36 {true, false, 0x80081441}, 37 {false, true, 0x80081481}, 38 {true, true, 0x800814c1}, 39 } { 40 t.Run(fmt.Sprintf("locked=%t/open=%t", scenario.locked, scenario.open), func(t *testing.T) { 41 door := worldDoor{template: 41565, locked: scenario.locked, open: scenario.open} 42 43 packet, err := doorPacket(9000001, door) 44 if err != nil { 45 t.Fatal(err) 46 } 47 packet = targetPacket(packet) 48 49 if binary.BigEndian.Uint32(packet[107:111]) != scenario.flags { 50 t.Fatal("door state changed unrelated template flags") 51 } 52 }) 53 } 54 } 55 56 func TestDoorFullUpdateAcceptsOriginalDoorCatalog(t *testing.T) { 57 count := 0 58 for template, definition := range gamedata.ItemTemplates { 59 if definition.HeaderWords[0] != 51016 { 60 continue 61 } 62 63 packet, err := doorPacket(9000001, worldDoor{template: template}) 64 65 if err != nil || len(packet) == 0 { 66 t.Fatalf("original door %d was rejected: %v", template, err) 67 } 68 count++ 69 } 70 if count != 53 { 71 t.Fatalf("original door catalog changed: %d", count) 72 } 73 } 74 75 func TestDoorFullUpdateRejectsUnknownAndNonDoorTemplates(t *testing.T) { 76 for _, template := range []uint32{0, 4294967295, 95577} { 77 t.Run(fmt.Sprint(template), func(t *testing.T) { 78 door := worldDoor{template: template} 79 80 packet, err := doorPacket(9000001, door) 81 82 if err == nil || packet != nil { 83 t.Fatal("invalid template produced a partial door update") 84 } 85 }) 86 } 87 } 88 89 func TestDoorFullUpdatePreservesAbsentAuthoredQuality(t *testing.T) { 90 for _, template := range []uint32{41553, 41554, 41892} { 91 t.Run(fmt.Sprint(template), func(t *testing.T) { 92 door := worldDoor{template: template} 93 if gamedata.ItemTemplates[template].Quality != nil { 94 t.Fatal("original unqualified door changed") 95 } 96 97 packet, err := doorPacket(9000001, door) 98 if err != nil { 99 t.Fatal(err) 100 } 101 packet = targetPacket(packet) 102 103 if binary.BigEndian.Uint32(packet[83:87]) != 3027 || 104 binary.BigEndian.Uint32(packet[87:91]) != 23 || binary.BigEndian.Uint32(packet[91:95]) != template || 105 binary.BigEndian.Uint32(packet[95:99]) != 0 { 106 t.Fatal("unqualified door acquired an invented QL override") 107 } 108 }) 109 } 110 } 111 112 func TestDoorStatusKeepsLockOpenAndLatchIndependent(t *testing.T) { 113 for _, scenario := range []struct { 114 locked, open, latch bool 115 body string 116 }{ 117 {false, false, false, "0000000200000000000000000003f1"}, 118 {true, false, false, "0000000201000000000000000003f1"}, 119 {false, true, false, "0000000200010000000000000003f1"}, 120 {true, true, false, "0000000201010000000000000003f1"}, 121 {false, false, true, "0000000200000000000001000003f1"}, 122 {true, false, true, "0000000201000000000001000003f1"}, 123 {false, true, true, "0000000200010000000001000003f1"}, 124 {true, true, true, "0000000201010000000001000003f1"}, 125 } { 126 t.Run(fmt.Sprintf("locked=%t/open=%t/latch=%t", scenario.locked, scenario.open, scenario.latch), func(t *testing.T) { 127 id := uint32(8000001) 128 129 packet := targetPacket(doorStatusPacket(id, scenario.locked, scenario.open, scenario.latch)) 130 packet = packet[:binary.BigEndian.Uint16(packet[6:8])] 131 132 if binary.BigEndian.Uint32(packet[16:20]) != 0x4c7d403b || 133 binary.BigEndian.Uint32(packet[20:24]) != 51016 || binary.BigEndian.Uint32(packet[24:28]) != 8000001 || 134 hex.EncodeToString(packet[29:]) != scenario.body { 135 t.Fatal("door status fields were merged, reordered or truncated", hex.EncodeToString(packet)) 136 } 137 }) 138 } 139 }