aorecomp

Anarchy Online classic in the browser

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


server/mission_world_test.go (8207B)


      1 package main
      2 
      3 import (
      4 	"math"
      5 	"sync"
      6 	"testing"
      7 )
      8 
      9 func fixedMissionLayout() missionLayout {
     10 	return missionLayout{template: 331, width: 4, depth: 6, floorHeight: 10,
     11 		rooms: []missionRoom{{template: 0, x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}}
     12 }
     13 
     14 func TestMissionWorldRegistersOriginalPortalLinksAtEveryRotation(t *testing.T) {
     15 	geometry := missionGeometryFixture(t)
     16 	for _, scenario := range []struct {
     17 		name         string
     18 		width, depth uint16
     19 		rooms        []missionRoom
     20 		first        [3]float32
     21 		second       [3]float32
     22 	}{
     23 		{"north", 4, 6, []missionRoom{{template: 0, x: 1, z: 3, rotation: 2}, {template: 37, x: 1, z: 1}}, [3]float32{15, 0, 29.999}, [3]float32{15, 0, 30.001}},
     24 		{"east", 6, 4, []missionRoom{{template: 0, x: 1, z: 1, rotation: 3}, {template: 37, x: 3, z: 1, rotation: 1}}, [3]float32{29.999, 0, 25}, [3]float32{30.001, 0, 25}},
     25 		{"south", 4, 6, []missionRoom{{template: 0, x: 2, z: 1}, {template: 37, x: 1, z: 3, rotation: 2}}, [3]float32{25, 0, 30.001}, [3]float32{25, 0, 29.999}},
     26 		{"west", 6, 4, []missionRoom{{template: 0, x: 3, z: 2, rotation: 1}, {template: 37, x: 1, z: 1, rotation: 3}}, [3]float32{30.001, 0, 15}, [3]float32{29.999, 0, 15}},
     27 	} {
     28 		t.Run(scenario.name, func(t *testing.T) {
     29 			layout := missionLayout{template: 331, width: scenario.width, depth: scenario.depth, floorHeight: 10, rooms: scenario.rooms}
     30 
     31 			if err := geometry.addMission(9000001, layout); err != nil {
     32 				t.Fatal(err)
     33 			}
     34 			scene := geometry.mission(9000001)
     35 			first, direction := scene.portal(&scene.rooms[0], 10)
     36 			second, opposite := scene.portal(&scene.rooms[1], 10)
     37 			geometry.removeMission(9000001)
     38 
     39 			if scene.entrance != &scene.rooms[0] || len(scene.links) != 1 || scene.links[0].rooms != [2]*placedRoom{&scene.rooms[0], &scene.rooms[1]} {
     40 				t.Fatal("original portal pairing or unique exterior room", scene.entrance, scene.links)
     41 			}
     42 			if surfaceDistanceSquared(first, scenario.first) > .000000001 || surfaceDistanceSquared(second, scenario.second) > .000000001 || (direction+2)&3 != opposite {
     43 				t.Fatal("native portal positions and opposing directions", first, second, direction, opposite)
     44 			}
     45 			if scene.links[0].position != first || scene.links[0].direction != direction {
     46 				t.Fatal("door replay lost the matched portal pose", scene.links[0])
     47 			}
     48 			if scene.roomAt(scene.safePosition()) != scene.entrance || geometry.physical(9000001) {
     49 				t.Fatal("safe room or retired world lookup", scene.safePosition())
     50 			}
     51 		})
     52 	}
     53 }
     54 
     55 func TestMissionWorldRejectsPublicDuplicateAndUnpairedLayouts(t *testing.T) {
     56 	geometry := missionGeometryFixture(t)
     57 	if err := geometry.addMission(9000001, fixedMissionLayout()); err != nil {
     58 		t.Fatal(err)
     59 	}
     60 	original := geometry.mission(9000001)
     61 	for _, scenario := range []struct {
     62 		name     string
     63 		identity uint32
     64 		change   func(*missionLayout)
     65 	}{
     66 		{"zero identity", 0, func(*missionLayout) {}},
     67 		{"public outdoor", 705, func(*missionLayout) {}},
     68 		{"public indoor", 3000, func(*missionLayout) {}},
     69 		{"duplicate", 9000001, func(*missionLayout) {}},
     70 		{"unpaired entrance", 9000002, func(layout *missionLayout) { layout.rooms = layout.rooms[:1] }},
     71 		{"misaligned portals", 9000002, func(layout *missionLayout) { layout.rooms[1].x = 2 }},
     72 		{"different authored floors", 9000002, func(layout *missionLayout) { layout.rooms[1].floor = 1 }},
     73 		{"nonopposing portals", 9000002, func(layout *missionLayout) { layout.rooms[1].rotation = 1 }},
     74 		{"ambiguous pair", 9000002, func(layout *missionLayout) { layout.rooms = append(layout.rooms, layout.rooms[1]) }},
     75 		{"no exterior", 9000002, func(layout *missionLayout) {
     76 			layout.rooms = []missionRoom{{template: 37, x: 1, z: 1}, {template: 37, x: 0, z: 3, rotation: 2}}
     77 		}},
     78 		{"unreachable closed pair", 9000002, func(layout *missionLayout) {
     79 			layout.width = 8
     80 			layout.rooms = append(layout.rooms, missionRoom{template: 37, x: 5, z: 1}, missionRoom{template: 37, x: 4, z: 3, rotation: 2})
     81 		}},
     82 	} {
     83 		t.Run(scenario.name, func(t *testing.T) {
     84 			layout := fixedMissionLayout()
     85 			scenario.change(&layout)
     86 
     87 			err := geometry.addMission(scenario.identity, layout)
     88 
     89 			if err == nil || geometry.mission(9000001) != original || geometry.mission(9000002) != nil {
     90 				t.Fatal("invalid registration changed live geometry", err)
     91 			}
     92 		})
     93 	}
     94 }
     95 
     96 func TestMissionWorldRoutesCommonQueriesWithoutOutdoorTerrainAndIsolatesInstances(t *testing.T) {
     97 	geometry := missionGeometryFixture(t)
     98 	if err := geometry.addMission(9000001, fixedMissionLayout()); err != nil {
     99 		t.Fatal(err)
    100 	}
    101 	shifted := fixedMissionLayout()
    102 	shifted.width, shifted.rooms[0].x, shifted.rooms[1].x = 6, 3, 3
    103 	if err := geometry.addMission(9000002, shifted); err != nil {
    104 		t.Fatal(err)
    105 	}
    106 	var point, normal [3]float32
    107 
    108 	wall := geometry.line(9000001, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true)
    109 	other := geometry.line(9000002, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true)
    110 	contact := geometry.sphere(9000001, [3]float32{17.1, .8, 16}, .25)
    111 	liquid := geometry.floor(9000002, nil, [3]float32{35, 1, 35}, &point, &normal)
    112 	first, second := geometry.mission(9000001), geometry.mission(9000002)
    113 	geometry.removeMission(9000001)
    114 	after := geometry.line(9000001, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true)
    115 
    116 	if !wall.hit || math.Abs(float64(wall.point[0]-17.601997)) > .00001 || other.hit || !contact.hit || after.hit {
    117 		t.Fatal("common queries missed mission dispatch or crossed world identities", wall, other, contact, after)
    118 	}
    119 	if point != [3]float32{35, .001, 35} || normal != [3]float32{0, 1, 0} || liquid != (waterContact{}) {
    120 		t.Fatal("dry mission floor must work without outdoor terrain", point, normal, liquid)
    121 	}
    122 	if first.rooms[0].surface != second.rooms[0].surface || &first.rooms[0] == &second.rooms[0] || !geometry.physical(9000002) || geometry.physical(9000001) {
    123 		t.Fatal("retirement must preserve the other instance and its shared immutable mesh")
    124 	}
    125 }
    126 
    127 func TestMissionWorldConcurrentRegistrationQueriesAndRetirement(t *testing.T) {
    128 	geometry := missionGeometryFixture(t)
    129 	if err := geometry.addMission(9000001, fixedMissionLayout()); err != nil {
    130 		t.Fatal(err)
    131 	}
    132 	var workers sync.WaitGroup
    133 
    134 	for range 4 {
    135 		workers.Go(func() {
    136 			for range 30 {
    137 				if !geometry.line(9000001, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true).hit {
    138 					t.Error("stable instance lost its wall")
    139 				}
    140 				geometry.line(9000002, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true)
    141 			}
    142 		})
    143 	}
    144 	for range 5 {
    145 		if err := geometry.addMission(9000002, fixedMissionLayout()); err != nil {
    146 			t.Fatal(err)
    147 		}
    148 		geometry.removeMission(9000002)
    149 	}
    150 	workers.Wait()
    151 
    152 	if !geometry.physical(9000001) || geometry.physical(9000002) {
    153 		t.Fatal("concurrent retirement crossed world identities")
    154 	}
    155 }
    156 
    157 func TestMissionWorldConcurrentRegistrationPreservesEveryIdentity(t *testing.T) {
    158 	geometry := missionGeometryFixture(t)
    159 	identities := []uint32{9000001, 9000002, 9000003, 9000004}
    160 	results := make(chan error, len(identities))
    161 	for _, identity := range identities {
    162 		go func() { results <- geometry.addMission(identity, fixedMissionLayout()) }()
    163 	}
    164 	for range identities {
    165 		if err := <-results; err != nil {
    166 			t.Error(err)
    167 		}
    168 	}
    169 
    170 	for _, identity := range identities {
    171 		if !geometry.physical(identity) || !geometry.line(identity, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true).hit {
    172 			t.Fatal("concurrent registration lost another mission scene", identity)
    173 		}
    174 	}
    175 }
    176 
    177 func TestMissionWorldNavigationViewRetainsItsRetiredScene(t *testing.T) {
    178 	geometry := missionGeometryFixture(t)
    179 	if err := geometry.addMission(9000001, fixedMissionLayout()); err != nil {
    180 		t.Fatal(err)
    181 	}
    182 	view := geometry.navigationGeometry(9000001)
    183 	geometry.removeMission(9000001)
    184 	shifted := fixedMissionLayout()
    185 	shifted.width, shifted.rooms[0].x, shifted.rooms[1].x = 6, 3, 3
    186 	if err := geometry.addMission(9000001, shifted); err != nil {
    187 		t.Fatal(err)
    188 	}
    189 
    190 	old := view.line(9000001, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true)
    191 	current := geometry.line(9000001, nil, [3]float32{15, 1, 16}, [3]float32{20, 1, 16}, true)
    192 	if !old.hit || current.hit {
    193 		t.Fatal("a retained navigation view changed to a replacement scene", old, current)
    194 	}
    195 }