aorecomp

Anarchy Online classic in the browser

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


server/surface_line_test.go (8139B)


      1 package main
      2 
      3 import (
      4 	"ao/server/assets"
      5 	"math"
      6 	"sync"
      7 	"testing"
      8 )
      9 
     10 func TestSurfaceLineUsesDirectedFacesAndStrictEndpoint(t *testing.T) {
     11 	tree := &surfaceTree{
     12 		vertices: [][3]float32{{0, 0, 0}, {0, 0, 2}, {0, 2, 0}},
     13 		groups:   []surfaceGroup{{bounds: [6]float32{-.01, 0, 0, .01, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}}},
     14 		nodes:    []surfaceNode{{groups: []uint32{0}}},
     15 	}
     16 
     17 	for _, scenario := range []struct {
     18 		name        string
     19 		origin, end [3]float32
     20 		hit         bool
     21 		point       [3]float32
     22 		fraction    float32
     23 	}{
     24 		{"face", [3]float32{-1, .5, .5}, [3]float32{1, .5, .5}, true, [3]float32{0, .5, .5}, .5},
     25 		{"edge", [3]float32{-1, 0, .5}, [3]float32{1, 0, .5}, true, [3]float32{0, 0, .5}, .5},
     26 		{"vertex", [3]float32{-1, 0, 0}, [3]float32{1, 0, 0}, true, [3]float32{}, .5},
     27 		{"origin included", [3]float32{0, .5, .5}, [3]float32{1, .5, .5}, true, [3]float32{0, .5, .5}, 0},
     28 		{"tiny negative fraction", [3]float32{.0000001, .5, .5}, [3]float32{1, .5, .5}, true, [3]float32{0, .5, .5}, -.00000010000001},
     29 		{"endpoint excluded", [3]float32{-1, .5, .5}, [3]float32{0, .5, .5}, false, [3]float32{}, 0},
     30 		{"back face", [3]float32{1, .5, .5}, [3]float32{-1, .5, .5}, false, [3]float32{}, 0},
     31 		{"parallel", [3]float32{-1, .5, .5}, [3]float32{-1, 1, .5}, false, [3]float32{}, 0},
     32 		{"outside triangle", [3]float32{-1, 1.5, 1.5}, [3]float32{1, 1.5, 1.5}, false, [3]float32{}, 0},
     33 	} {
     34 		t.Run(scenario.name, func(t *testing.T) {
     35 			contact := tree.line(scenario.origin, scenario.end)
     36 			if contact.hit != scenario.hit {
     37 				t.Fatal("directed segment eligibility", contact)
     38 			}
     39 			if contact.hit && (contact.normal != [3]float32{-1, 0, 0} || contact.point != scenario.point || math.Abs(float64(contact.fraction-scenario.fraction)) > .00000001) {
     40 				t.Fatal("directed segment point/normal/fraction", contact)
     41 			}
     42 		})
     43 	}
     44 }
     45 
     46 func TestSurfaceLineLetsLaterGroupsReplaceEqualHits(t *testing.T) {
     47 	vertices := [][3]float32{{0, -2, -2}, {0, -2, 2}, {0, 2, -2}, {-2, -2, 0}, {-2, 2, 0}, {2, -2, 0}}
     48 	groups := []surfaceGroup{
     49 		{bounds: [6]float32{0, -2, -2, 0, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}},
     50 		{bounds: [6]float32{-2, -2, 0, 2, 2, 0}, triangles: [][3]uint32{{3, 4, 5}}},
     51 	}
     52 	oneGroup := &surfaceTree{vertices: vertices,
     53 		groups: []surfaceGroup{{bounds: [6]float32{-2, -2, -2, 2, 2, 2}, triangles: [][3]uint32{{0, 1, 2}, {3, 4, 5}}}},
     54 		nodes:  []surfaceNode{{groups: []uint32{0}}}}
     55 
     56 	firstTriangle := oneGroup.line([3]float32{-1, 0, -1}, [3]float32{1, 0, 1})
     57 	if !firstTriangle.hit || firstTriangle.normal != [3]float32{-1, 0, 0} {
     58 		t.Fatal("equal triangles inside one group must retain the first", firstTriangle)
     59 	}
     60 	for _, scenario := range []struct {
     61 		name   string
     62 		nodes  []surfaceNode
     63 		normal [3]float32
     64 	}{
     65 		{"stored order", []surfaceNode{{groups: []uint32{0, 1}}}, [3]float32{0, 0, -1}},
     66 		{"reversed order", []surfaceNode{{groups: []uint32{1, 0}}}, [3]float32{-1, 0, 0}},
     67 		{"KD split equality", []surfaceNode{{axis: 0, children: [2]int{1, 2}}, {groups: []uint32{0}}, {groups: []uint32{1}}}, [3]float32{0, 0, -1}},
     68 	} {
     69 		t.Run(scenario.name, func(t *testing.T) {
     70 			tree := &surfaceTree{vertices: vertices, groups: append([]surfaceGroup(nil), groups...), nodes: scenario.nodes}
     71 			contact := tree.line([3]float32{-1, 0, -1}, [3]float32{1, 0, 1})
     72 			if !contact.hit || contact.fraction != .5 || contact.normal != scenario.normal || contact.point != [3]float32{} {
     73 				t.Fatal("native cross-group tie replacement", contact)
     74 			}
     75 		})
     76 	}
     77 }
     78 
     79 func TestSceneQueriesIncludeInvisibleGeometryAndItsEqualHits(t *testing.T) {
     80 	surface := &sceneSurface{
     81 		primary: &surfaceTree{
     82 			vertices: [][3]float32{{0, -2, -2}, {0, -2, 2}, {0, 2, -2}},
     83 			groups:   []surfaceGroup{{bounds: [6]float32{0, -2, -2, 0, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}}},
     84 			nodes:    []surfaceNode{{groups: []uint32{0}}},
     85 		},
     86 		invisible: &surfaceTree{
     87 			vertices: [][3]float32{{-2, -2, 0}, {-2, 2, 0}, {2, -2, 0}},
     88 			groups:   []surfaceGroup{{bounds: [6]float32{-2, -2, 0, 2, 2, 0}, triangles: [][3]uint32{{0, 1, 2}}}},
     89 			nodes:    []surfaceNode{{groups: []uint32{0}}},
     90 		},
     91 	}
     92 
     93 	equalSphere := surface.sphere([3]float32{-.25, 0, -.25}, .5)
     94 	nearSphere := surface.sphere([3]float32{-.1, 0, -.25}, .5)
     95 	equalLine := surface.line([3]float32{-1, 0, -1}, [3]float32{1, 0, 1})
     96 	nearLine := surface.line([3]float32{-.25, -.75, -1}, [3]float32{1, -.75, 1})
     97 
     98 	if !equalSphere.hit || equalSphere.normal != [3]float32{0, 0, -1} || !equalLine.hit || equalLine.normal != [3]float32{0, 0, -1} {
     99 		t.Fatal("extra invisible geometry must win exact distance ties", equalSphere, equalLine)
    100 	}
    101 	if !nearSphere.hit || nearSphere.normal != [3]float32{-1, 0, 0} || !nearLine.hit || nearLine.normal != [3]float32{-1, 0, 0} {
    102 		t.Fatal("strictly nearer primary geometry must replace invisible geometry", nearSphere, nearLine)
    103 	}
    104 }
    105 
    106 func TestSurfaceLineFloorAndSphereShareLazyCoefficients(t *testing.T) {
    107 	tree := &surfaceTree{
    108 		vertices: [][3]float32{{0, 0, 0}, {0, 0, 2}, {0, 2, 0}, {-2, 0, -2}, {-2, 0, 2}, {2, 0, -2}},
    109 		groups: []surfaceGroup{
    110 			{bounds: [6]float32{0, 0, 0, 0, 2, 2}, triangles: [][3]uint32{{0, 1, 2}}},
    111 			{bounds: [6]float32{-2, 0, -2, 2, 0, 2}, triangles: [][3]uint32{{3, 4, 5}}},
    112 		},
    113 		nodes: []surfaceNode{{groups: []uint32{0, 1}}},
    114 	}
    115 	var workers sync.WaitGroup
    116 	results := make(chan [3]bool, 16)
    117 
    118 	for range 16 {
    119 		workers.Add(1)
    120 		go func() {
    121 			defer workers.Done()
    122 			sphere := tree.sphere([3]float32{-.25, .5, .5}, .5)
    123 			line := tree.line([3]float32{-1, .5, .5}, [3]float32{1, .5, .5})
    124 			floor := tree.floor([3]float32{-.5, 1, -.5}, 2)
    125 			results <- [3]bool{sphere.hit && sphere.distance == .25, line.hit && line.fraction == .5, floor.hit && floor.point[1] == 0}
    126 		}()
    127 	}
    128 	workers.Wait()
    129 	close(results)
    130 
    131 	for result := range results {
    132 		if result != [3]bool{true, true, true} {
    133 			t.Fatal("concurrent shared geometry queries", result)
    134 		}
    135 	}
    136 }
    137 
    138 func TestSurfaceFloorKeepsNativeBoundAndMissSemantics(t *testing.T) {
    139 	tree := &surfaceTree{
    140 		vertices: [][3]float32{{-2, 1.8, -2}, {-2, 1.8, 2}, {2, 1.8, -2}},
    141 		groups:   []surfaceGroup{{bounds: [6]float32{-2, 1.8, -2, 2, 1.8, 2}, triangles: [][3]uint32{{0, 1, 2}}}},
    142 		nodes:    []surfaceNode{{groups: []uint32{0}}},
    143 	}
    144 
    145 	for _, scenario := range []struct {
    146 		name     string
    147 		origin   [3]float32
    148 		bound    float32
    149 		hit      bool
    150 		y        float32
    151 		fraction float32
    152 	}{
    153 		{"ordinary floor", [3]float32{0, 2, -.5}, 2, true, 1.8, .100000024},
    154 		{"mixed scalar gate", [3]float32{0, 2, -.5}, .25, false, 0, 0},
    155 		{"endpoint excluded", [3]float32{0, 2.8, -.5}, 1, false, 0, 0},
    156 		{"outside group", [3]float32{4, 2, -.5}, 2, false, 0, 0},
    157 		{"below floor", [3]float32{0, 1, -.5}, 2, false, 0, 0},
    158 	} {
    159 		t.Run(scenario.name, func(t *testing.T) {
    160 			contact := tree.floor(scenario.origin, scenario.bound)
    161 			if contact.hit != scenario.hit || contact.point != [3]float32{scenario.origin[0], scenario.y, scenario.origin[2]} || contact.normal != [3]float32{0, 1, 0} {
    162 				t.Fatal("native floor result or zero-height miss", contact)
    163 			}
    164 			if contact.hit && math.Abs(float64(contact.fraction-scenario.fraction)) > .0000001 {
    165 				t.Fatal("native floor scalar", contact.fraction)
    166 			}
    167 		})
    168 	}
    169 }
    170 
    171 func TestSurfaceLineAndFloorFindShippedWallAndRoof(t *testing.T) {
    172 	database, err := readGameDatabase(assets.Files)
    173 	if err != nil {
    174 		t.Fatal(err)
    175 	}
    176 	t.Cleanup(func() { database.file.Close() })
    177 	payload, version, err := database.read(1000013, 46203264)
    178 	if err != nil {
    179 		t.Fatal(err)
    180 	}
    181 	surface, err := decodeSceneSurface(payload, version)
    182 	if err != nil {
    183 		t.Fatal(err)
    184 	}
    185 
    186 	wall := surface.line([3]float32{970, 17.5, 520.2}, [3]float32{975, 17.5, 520.2})
    187 	roof := surface.primary.floor([3]float32{972.2, 22, 520.2}, 6)
    188 
    189 	if !wall.hit || wall.normal != [3]float32{-1, 0, 0} || math.Abs(float64(wall.point[0]-971.9442138671875)) > .0001 {
    190 		t.Fatal("shipped building directed wall", wall)
    191 	}
    192 	if !roof.hit || math.Abs(float64(roof.normal[1]-.98302039306738)) > .000001 || math.Abs(float64(roof.normal[2]+.18349634005520044)) > .000001 || math.Abs(float64(roof.point[1]-21.192259481797624)) > .0001 {
    193 		t.Fatal("shipped building roof floor selection", roof)
    194 	}
    195 }