aorecomp

Anarchy Online classic in the browser

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


server/terrain_line_test.go (4082B)


      1 package main
      2 
      3 import (
      4 	"math"
      5 	"slices"
      6 	"testing"
      7 )
      8 
      9 func TestGridLinePreservesNativeCellVisitation(t *testing.T) {
     10 	for _, scenario := range []struct {
     11 		name         string
     12 		start, delta [2]float64
     13 		end          [2]int
     14 		cells        [][2]int
     15 	}{
     16 		{"diagonal", [2]float64{.5, .5}, [2]float64{2, 2}, [2]int{2, 2}, [][2]int{{0, 0}, {0, 1}, {1, 1}, {1, 2}, {2, 2}}},
     17 		{"reverse", [2]float64{2.5, 2.5}, [2]float64{-2, -2}, [2]int{0, 0}, [][2]int{{2, 2}, {2, 1}, {1, 1}, {1, 0}, {0, 0}}},
     18 		{"one axis", [2]float64{.5, .5}, [2]float64{0, 2}, [2]int{0, 2}, [][2]int{{0, 0}, {0, 1}, {0, 2}}},
     19 		{"stationary", [2]float64{.5, .5}, [2]float64{}, [2]int{0, 0}, [][2]int{{0, 0}}},
     20 		{"negative fractional start", [2]float64{-.5, .5}, [2]float64{2, 0}, [2]int{1, 0}, [][2]int{{0, 0}, {1, 0}}},
     21 	} {
     22 		t.Run(scenario.name, func(t *testing.T) {
     23 			var visited [][2]int
     24 			for line := newGridLine(scenario.start, scenario.delta, scenario.end); line.remaining > 0; line.advance() {
     25 				visited = append(visited, line.cell)
     26 			}
     27 
     28 			if !slices.Equal(visited, scenario.cells) {
     29 				t.Fatal("native Z-first crossing ties or truncation", visited)
     30 			}
     31 		})
     32 	}
     33 }
     34 
     35 func TestTerrainLineUsesNativeTriangleOrderAndNormals(t *testing.T) {
     36 	ground := &terrain{width: 3, height: 3, boundsWidth: 3, boundsHeight: 3, spacing: 4, scale: 1,
     37 		heights: []byte{0, 0, 0, 99, 0, 4, 0, 99, 0, 0, 0, 99, 99, 99, 99, 99}}
     38 
     39 	for _, scenario := range []struct {
     40 		name         string
     41 		x, z, height float32
     42 		normal       [3]float32
     43 	}{
     44 		{"AC first half", 3, .5, .5, [3]float32{0, .70710677, -.70710677}},
     45 		{"AC second half", .5, 3, .5, [3]float32{-.70710677, .70710677, 0}},
     46 		{"AC ray tie uses first half", 2, 2, 2, [3]float32{0, .70710677, -.70710677}},
     47 		{"BD first half", 4.5, .5, .5, [3]float32{0, .70710677, -.70710677}},
     48 		{"BD second half", 7.5, 3.5, .5, [3]float32{.70710677, .70710677, 0}},
     49 		{"repeated far samples", 11.75, 11.75, 0, [3]float32{0, 1, 0}},
     50 	} {
     51 		t.Run(scenario.name, func(t *testing.T) {
     52 			origin, end := [3]float32{scenario.x, 10, scenario.z}, [3]float32{scenario.x, -10, scenario.z}
     53 			contact := ground.line(origin, end, [3]float32{0, -20, 0})
     54 			if !contact.hit || math.Abs(float64(contact.point[1]-scenario.height)) > .000001 {
     55 				t.Fatal("native terrain plane intersection", contact)
     56 			}
     57 			for axis := range 3 {
     58 				if math.Abs(float64(contact.normal[axis]-scenario.normal[axis])) > .000001 {
     59 					t.Fatal("native terrain triangle normal", contact.normal)
     60 				}
     61 			}
     62 		})
     63 	}
     64 }
     65 
     66 func TestTerrainLinePreservesDistanceAndDirectionGates(t *testing.T) {
     67 	ground := &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 4, scale: 1, heights: make([]byte, 9)}
     68 
     69 	for _, scenario := range []struct {
     70 		name       string
     71 		start, end float32
     72 		hit        bool
     73 	}{
     74 		{"downward", 1, -1, true},
     75 		{"endpoint excluded", 1, 0, false},
     76 		{"upward excluded", -1, 1, false},
     77 		{"both above tile", 2, 1, false},
     78 		{"short behind-origin hit", -.5, -2, true},
     79 		{"distant behind-origin miss", -2, -2.5, false},
     80 	} {
     81 		t.Run(scenario.name, func(t *testing.T) {
     82 			contact := ground.line([3]float32{2, scenario.start, 2}, [3]float32{2, scenario.end, 2}, [3]float32{0, scenario.end - scenario.start, 0})
     83 			if contact.hit != scenario.hit || contact.hit && contact.point != [3]float32{2, 0, 2} {
     84 				t.Fatal("terrain direction or strict unsigned distance gate", contact)
     85 			}
     86 		})
     87 	}
     88 }
     89 
     90 func TestTerrainSampleKeepsPhysicalNormalAtDiagonalTie(t *testing.T) {
     91 	ground := &terrain{width: 2, height: 2, boundsWidth: 2, boundsHeight: 2, spacing: 4, scale: 1,
     92 		heights: []byte{0, 0, 99, 0, 4, 99, 99, 99, 99}}
     93 
     94 	height, normal, inside := ground.sample(2, 2)
     95 	_, outsideNormal, outside := ground.sample(8, 2)
     96 
     97 	if !inside || height != 2 || math.Abs(float64(normal[0]+.70710677)) > .000001 || math.Abs(float64(normal[1]-.70710677)) > .000001 || normal[2] != 0 {
     98 		t.Fatal("height query uses its own second-half tie normal", height, normal, inside)
     99 	}
    100 	if outside || outsideNormal != [3]float32{} {
    101 		t.Fatal("outside terrain must not invent a valid normal", outsideNormal, outside)
    102 	}
    103 }