server/terrain.go (4646B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "io/fs" 7 "math" 8 ) 9 10 type terrain struct { 11 width, height int 12 boundsWidth, boundsHeight int 13 spacing, scale float32 14 heights []byte 15 } 16 17 func readTerrain() (map[uint32]*terrain, error) { 18 data, err := fs.ReadFile(resources, "terrain.bin") 19 if err != nil { 20 return nil, err 21 } 22 if len(data) < 12 || string(data[:8]) != "AOTERR02" { 23 return nil, fmt.Errorf("invalid terrain file header") 24 } 25 count := binary.LittleEndian.Uint32(data[8:12]) 26 data = data[12:] 27 maps := make(map[uint32]*terrain) 28 for range count { 29 if len(data) < 20 { 30 return nil, fmt.Errorf("truncated terrain record") 31 } 32 id := binary.LittleEndian.Uint32(data[:4]) 33 ground := &terrain{ 34 width: int(binary.LittleEndian.Uint16(data[4:6])), height: int(binary.LittleEndian.Uint16(data[6:8])), 35 boundsWidth: int(binary.LittleEndian.Uint16(data[8:10])), boundsHeight: int(binary.LittleEndian.Uint16(data[10:12])), 36 spacing: math.Float32frombits(binary.LittleEndian.Uint32(data[12:16])), 37 scale: math.Float32frombits(binary.LittleEndian.Uint32(data[16:20])), 38 } 39 data = data[20:] 40 size := (ground.width + 1) * (ground.height + 1) 41 if ground.width == 0 || ground.height == 0 || ground.boundsWidth == 0 || ground.boundsWidth > ground.width || ground.boundsHeight == 0 || ground.boundsHeight > ground.height || !(ground.spacing > 0 && ground.spacing < math.MaxFloat32) || !(ground.scale > 0 && ground.scale < math.MaxFloat32) || len(data) < size || maps[id] != nil { 42 return nil, fmt.Errorf("invalid terrain record for playfield %d", id) 43 } 44 ground.heights = data[:size] 45 maps[id] = ground 46 data = data[size:] 47 } 48 if len(data) != 0 { 49 return nil, fmt.Errorf("unexpected bytes after terrain records") 50 } 51 return maps, nil 52 } 53 54 func (ground *terrain) elevation(x, z float32) (float32, bool) { 55 // LLR GetHeightAndNormalAtPos uses header bounds, not the padded archive grid. 56 x, z = x/ground.spacing, z/ground.spacing 57 if !(x >= 0 && x < float32(ground.boundsWidth) && z >= 0 && z < float32(ground.boundsHeight)) { 58 return 0, false 59 } 60 column, row := int(x), int(z) 61 x, z = x-float32(column), z-float32(row) 62 nextColumn, nextRow := min(column+1, ground.boundsWidth-1), min(row+1, ground.boundsHeight-1) 63 topLeft := float32(ground.heights[row*(ground.width+1)+column]) * ground.scale 64 topRight := float32(ground.heights[row*(ground.width+1)+nextColumn]) * ground.scale 65 bottomRight := float32(ground.heights[nextRow*(ground.width+1)+nextColumn]) * ground.scale 66 bottomLeft := float32(ground.heights[nextRow*(ground.width+1)+column]) * ground.scale 67 68 // The physical surface uses alternating triangle planes, with unsmoothed edges. 69 // Source: client/game/llr.dll/lifted-0021.c, sub_380385E0/38038370. 70 if (column+row)&1 == 0 { 71 if x > z { 72 return topLeft + x*(topRight-topLeft) + z*(bottomRight-topRight), true 73 } 74 return topLeft + x*(bottomRight-bottomLeft) + z*(bottomLeft-topLeft), true 75 } 76 if x+z <= 1 { 77 return topLeft + x*(topRight-topLeft) + z*(bottomLeft-topLeft), true 78 } 79 return bottomRight + (1-x)*(bottomLeft-bottomRight) + (1-z)*(topRight-bottomRight), true 80 } 81 82 func (ground *terrain) corners(column, row int) [4][3]float32 { 83 // Source: client/game/llr.dll/lifted-0021.c, sub_38038370. 84 nextColumn, nextRow := min(column+1, ground.boundsWidth-1), min(row+1, ground.boundsHeight-1) 85 x, z := float32(column)*ground.spacing, float32(row)*ground.spacing 86 return [4][3]float32{ 87 {x, float32(ground.heights[row*(ground.width+1)+column]) * ground.scale, z}, 88 {x + ground.spacing, float32(ground.heights[row*(ground.width+1)+nextColumn]) * ground.scale, z}, 89 {x + ground.spacing, float32(ground.heights[nextRow*(ground.width+1)+nextColumn]) * ground.scale, z + ground.spacing}, 90 {x, float32(ground.heights[nextRow*(ground.width+1)+column]) * ground.scale, z + ground.spacing}, 91 } 92 } 93 94 func (ground *terrain) sample(x, z float32) (float32, [3]float32, bool) { 95 height, inside := ground.elevation(x, z) 96 if !inside { 97 return 0, [3]float32{}, false 98 } 99 column, row := int(x/ground.spacing), int(z/ground.spacing) 100 u, v := x/ground.spacing-float32(column), z/ground.spacing-float32(row) 101 indices := [3]int{0, 2, 3} 102 if (column+row)&1 == 0 { 103 if u > v { 104 indices = [3]int{0, 1, 2} 105 } 106 } else if u+v <= 1 { 107 indices = [3]int{0, 1, 3} 108 } else { 109 indices = [3]int{1, 2, 3} 110 } 111 corners := ground.corners(column, row) 112 var first, second [3]float32 113 for axis := range 3 { 114 first[axis] = corners[indices[1]][axis] - corners[indices[0]][axis] 115 second[axis] = corners[indices[2]][axis] - corners[indices[0]][axis] 116 } 117 return height, surfaceNormal(second, first), true 118 }