server/mission_geometry_test.go (4218B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "encoding/hex" 6 "fmt" 7 "testing" 8 ) 9 10 func TestMissionBuildingPacketKeepsGeneratorAndWorldOrigins(t *testing.T) { 11 player := &character{id: 1, playfield: 9000001, position: [3]float32{16, 1, 16}} 12 layout := missionLayout{template: 331, width: 4, depth: 4, floorHeight: 10, 13 ambient: [3]byte{30, 40, 50}, rooms: []missionRoom{{template: 0, x: 1, z: 1}, {template: 1, floor: -1, x: 2, z: 3, rotation: 3}}} 14 15 packet, err := missionPlayfieldPacket(player, layout) 16 if err != nil { 17 t.Fatal(err) 18 } 19 packet = targetPacket(packet) 20 packet = packet[:binary.BigEndian.Uint16(packet[6:8])] 21 22 if len(packet) != 121 { 23 t.Fatalf("generator was truncated or retained the legacy name: %d", len(packet)) 24 } 25 if got := hex.EncodeToString(packet[29:]); got != "00000004418000003f80000041800000610000c7a100895441008954410000000000009c50008954410000c79f0089544100000000000300040004000a0000014b1e2832000000020000000101000001ff0203030000000000000000" { 26 t.Fatalf("native generator, room placement or origins changed: %s", got) 27 } 28 } 29 30 func TestMissionBuildingPacketRejectsInvalidGeneratorContracts(t *testing.T) { 31 for _, scenario := range []struct { 32 name string 33 change func(*missionLayout) 34 }{ 35 {"no template", func(layout *missionLayout) { layout.template = 0 }}, 36 {"zero width", func(layout *missionLayout) { layout.width = 0 }}, 37 {"zero depth", func(layout *missionLayout) { layout.depth = 0 }}, 38 {"zero floor height", func(layout *missionLayout) { layout.floorHeight = 0 }}, 39 {"high floor", func(layout *missionLayout) { layout.floorHeight = 1001 }}, 40 {"no rooms", func(layout *missionLayout) { layout.rooms = nil }}, 41 {"many rooms", func(layout *missionLayout) { layout.rooms = make([]missionRoom, 10001) }}, 42 {"room index", func(layout *missionLayout) { layout.rooms[0].template = 10001 }}, 43 {"low floor", func(layout *missionLayout) { layout.rooms[0].floor = -17 }}, 44 {"high floor", func(layout *missionLayout) { layout.rooms[0].floor = 17 }}, 45 {"room x", func(layout *missionLayout) { layout.rooms[0].x = 33 }}, 46 {"room z", func(layout *missionLayout) { layout.rooms[0].z = 33 }}, 47 {"room rotation", func(layout *missionLayout) { layout.rooms[0].rotation = 4 }}, 48 } { 49 t.Run(scenario.name, func(t *testing.T) { 50 layout := missionLayout{template: 331, width: 4, depth: 4, floorHeight: 10, rooms: []missionRoom{{}}} 51 scenario.change(&layout) 52 53 packet, err := missionPlayfieldPacket(&character{playfield: 9000001}, layout) 54 55 if err == nil || packet != nil { 56 t.Fatal("invalid generator produced a partial native packet") 57 } 58 }) 59 } 60 } 61 62 func TestMissionBuildingPacketKeepsVariableRoomCountsAndOrigins(t *testing.T) { 63 for _, scenario := range []struct{ rooms, length int }{{1, 115}, {2, 121}, {10000, 60109}} { 64 t.Run(fmt.Sprint(scenario.rooms), func(t *testing.T) { 65 layout := missionLayout{template: 331, width: 32, depth: 32, floorHeight: 1000, rooms: make([]missionRoom, scenario.rooms)} 66 layout.rooms[0] = missionRoom{template: 10000, floor: -16, x: 32, z: 32, rotation: 3} 67 packet, err := missionPlayfieldPacket(&character{playfield: 9000001}, layout) 68 if err != nil { 69 t.Fatal(err) 70 } 71 end := int(binary.BigEndian.Uint16(packet[6:8])) 72 binary.BigEndian.PutUint32(packet[end-12:end-8], 123) 73 binary.BigEndian.PutUint32(packet[end-8:end-4], 456) 74 75 packet = targetPacket(packet) 76 packet = packet[:binary.BigEndian.Uint16(packet[6:8])] 77 78 if len(packet) != scenario.length || hex.EncodeToString(packet[len(packet)-8:]) != "0000007b000001c8" { 79 t.Fatal("native conversion lost rooms or moved the world origins", len(packet)) 80 } 81 if got := hex.EncodeToString(packet[101:107]); got != "2710f0202003" { 82 t.Fatal("valid room boundaries changed", got) 83 } 84 }) 85 } 86 } 87 88 func TestMissionBuildingPacketKeepsUnsignedDimensions(t *testing.T) { 89 layout := missionLayout{template: 331, width: 65535, depth: 32768, floorHeight: 10, rooms: []missionRoom{{}}} 90 91 packet, err := missionPlayfieldPacket(&character{playfield: 9000001}, layout) 92 if err != nil { 93 t.Fatal(err) 94 } 95 packet = targetPacket(packet) 96 97 if got := hex.EncodeToString(packet[84:90]); got != "ffff8000000a" { 98 t.Fatal("unsigned generator dimensions changed", got) 99 } 100 }