aorecomp

Anarchy Online classic in the browser

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


server/scene_water.go (11194B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"fmt"
      6 	"io"
      7 	"math"
      8 )
      9 
     10 type waterVolume struct {
     11 	vertices   [3][3]float32
     12 	normal     [3]float32
     13 	plane      float32
     14 	bounds     [6]float32
     15 	kind       uint32
     16 	clipOffset float32
     17 }
     18 
     19 type waterZone struct {
     20 	volumes   []int
     21 	maxHeight float32
     22 }
     23 
     24 type sceneWater struct {
     25 	volumes   []waterVolume
     26 	zones     []waterZone
     27 	maxHeight float32
     28 }
     29 
     30 type waterContact struct {
     31 	height float32
     32 	normal [3]float32
     33 	kind   uint32
     34 }
     35 
     36 func readSceneWater(data []byte, layout scenePlayfield, ground *terrain) (*sceneWater, error) {
     37 	// Source: client/game/kkr.dll/lifted-0096.c, sub_3614FEA0.
     38 	reader := &surfaceReader{Reader: bytes.NewReader(data)}
     39 	var header [13]uint32
     40 	reader.read(&header)
     41 	if reader.err != nil || header[0] != 8 || header[1] != header[10] || header[11] == 0 || uint64(header[12]) != uint64(layout.cellsX)*uint64(layout.cellsZ) {
     42 		return nil, fmt.Errorf("invalid outdoor water header")
     43 	}
     44 	if uint64(header[11]) > uint64(ground.boundsWidth) || uint64(header[11]) > uint64(ground.boundsHeight) || ground.boundsWidth/int(header[11]) != layout.cellsX || ground.boundsHeight/int(header[11]) != layout.cellsZ {
     45 		return nil, fmt.Errorf("outdoor water zones do not match terrain")
     46 	}
     47 	for range header[12] {
     48 		var count uint32
     49 		reader.read(&count)
     50 		if reader.err != nil || uint64(count)*44 > uint64(reader.Len()) {
     51 			return nil, fmt.Errorf("truncated outdoor zone objects")
     52 		}
     53 		_, reader.err = reader.Seek(int64(count)*44, io.SeekCurrent)
     54 	}
     55 	var count uint32
     56 	reader.read(&count)
     57 	if reader.err != nil || uint64(count)*12 > uint64(reader.Len()) {
     58 		return nil, fmt.Errorf("invalid outdoor water polygon count")
     59 	}
     60 	water := &sceneWater{zones: make([]waterZone, header[12])}
     61 	for index := range water.zones {
     62 		water.zones[index].maxHeight = -1
     63 	}
     64 	for range count {
     65 		var flags, verticesCount, facesCount uint32
     66 		reader.read(&flags)
     67 		reader.read(&verticesCount)
     68 		if reader.err != nil || uint64(verticesCount)*12 > uint64(reader.Len()) {
     69 			return nil, fmt.Errorf("truncated outdoor water vertices")
     70 		}
     71 		vertices := make([][3]float32, verticesCount)
     72 		reader.read(vertices)
     73 		for _, vertex := range vertices {
     74 			for _, value := range vertex {
     75 				if math.IsNaN(float64(value)) || math.IsInf(float64(value), 0) {
     76 					return nil, fmt.Errorf("nonfinite outdoor water vertex")
     77 				}
     78 			}
     79 		}
     80 		reader.read(&facesCount)
     81 		if reader.err != nil || uint64(facesCount)*6 > uint64(reader.Len()) {
     82 			return nil, fmt.Errorf("truncated outdoor water faces")
     83 		}
     84 		faces := make([][3]uint16, facesCount)
     85 		reader.read(faces)
     86 		for _, face := range faces {
     87 			for _, index := range face {
     88 				if uint32(index) >= verticesCount {
     89 					return nil, fmt.Errorf("invalid outdoor water face index")
     90 				}
     91 			}
     92 		}
     93 		if flags&1 != 0 || verticesCount == 3 {
     94 			for _, face := range faces {
     95 				water.create([3][3]float32{vertices[face[0]], vertices[face[1]], vertices[face[2]]}, flags)
     96 			}
     97 		} else if verticesCount != 0 {
     98 			var center [3]float32
     99 			for _, vertex := range vertices {
    100 				for axis := range 3 {
    101 					center[axis] += vertex[axis]
    102 				}
    103 			}
    104 			for axis := range 3 {
    105 				center[axis] /= float32(verticesCount)
    106 			}
    107 			for index, vertex := range vertices {
    108 				water.create([3][3]float32{center, vertices[(index+len(vertices)-1)%len(vertices)], vertex}, flags)
    109 			}
    110 		}
    111 	}
    112 	if reader.err != nil {
    113 		return nil, reader.err
    114 	}
    115 	// Later playfield fields follow the water extension; they are not part of this reader.
    116 	water.assignZones(ground, header[11])
    117 	return water, nil
    118 }
    119 
    120 func waterMidpoint(first, second [3]float32) [3]float32 {
    121 	var midpoint [3]float32
    122 	for axis := range 3 {
    123 		delta := float32(first[axis] - second[axis])
    124 		midpoint[axis] = delta/2 + second[axis]
    125 	}
    126 	return midpoint
    127 }
    128 
    129 func (water *sceneWater) create(vertices [3][3]float32, flags uint32) {
    130 	// Sources: client/game/bbr.dll/lifted-0017.c, sub_25052D20/25053210/25053A90.
    131 	if vertices[0] == vertices[1] || vertices[0] == vertices[2] || vertices[1] == vertices[2] {
    132 		return
    133 	}
    134 	kind := flags & 31
    135 	clipOffset := float32(int32(flags)>>5) * float32(.1)
    136 	if clipOffset == 0 {
    137 		clipOffset = 100000
    138 	}
    139 	minimum := min(vertices[0][1], vertices[1][1], vertices[2][1])
    140 	height := max(vertices[0][1], vertices[1][1], vertices[2][1]) - minimum
    141 	if kind&1 == 0 || height < .1 {
    142 		water.subdivide(vertices, kind, clipOffset)
    143 		return
    144 	}
    145 	for index := range vertices {
    146 		if (vertices[index][1]-minimum)/height > .5 {
    147 			vertices[index][1] = 1
    148 		} else {
    149 			vertices[index][1] = -.01
    150 		}
    151 	}
    152 	if vertices[0][1] == vertices[2][1] {
    153 		vertices[1], vertices[2] = vertices[2], vertices[1]
    154 	} else if vertices[0][1] != vertices[1][1] {
    155 		vertices = [3][3]float32{vertices[1], vertices[2], vertices[0]}
    156 	}
    157 	bend := func(vertex [3]float32) [3]float32 {
    158 		t := 1 - float64(vertex[1])
    159 		vertex[1] = float32((1-t*t*t)*float64(height) + float64(minimum))
    160 		return vertex
    161 	}
    162 	first, second := vertices[0], vertices[1]
    163 	tipHigh := vertices[2][1] > .5
    164 	if tipHigh {
    165 		first, second = vertices[2], vertices[2]
    166 	}
    167 	var stepFirst, stepSecond [3]float32
    168 	for axis := range 3 {
    169 		if tipHigh {
    170 			stepFirst[axis] = (vertices[0][axis] - vertices[2][axis]) * float32(.1)
    171 			stepSecond[axis] = (vertices[1][axis] - vertices[2][axis]) * float32(.1)
    172 		} else {
    173 			stepFirst[axis] = (vertices[2][axis] - vertices[0][axis]) * float32(.1)
    174 			stepSecond[axis] = (vertices[2][axis] - vertices[1][axis]) * float32(.1)
    175 		}
    176 	}
    177 	for strip := range 10 {
    178 		previousFirst, previousSecond := first, second
    179 		for axis := range 3 {
    180 			first[axis] += stepFirst[axis]
    181 			second[axis] += stepSecond[axis]
    182 		}
    183 		if tipHigh || strip != 9 {
    184 			water.addVolume([3][3]float32{bend(previousFirst), bend(first), bend(second)}, kind, clipOffset)
    185 		}
    186 		if !tipHigh || strip != 0 {
    187 			water.addVolume([3][3]float32{bend(previousFirst), bend(previousSecond), bend(second)}, kind, clipOffset)
    188 		}
    189 	}
    190 }
    191 
    192 func (water *sceneWater) subdivide(vertices [3][3]float32, kind uint32, clipOffset float32) {
    193 	var first, second [3]float32
    194 	for axis := range 3 {
    195 		first[axis] = vertices[1][axis] - vertices[0][axis]
    196 		second[axis] = vertices[2][axis] - vertices[0][axis]
    197 	}
    198 	normal := surfaceCross(first, second)
    199 	if kind&1 == 0 && math.Sqrt(surfaceDot(normal, normal)) > 10000 {
    200 		ab, ac, bc := waterMidpoint(vertices[1], vertices[0]), waterMidpoint(vertices[2], vertices[0]), waterMidpoint(vertices[2], vertices[1])
    201 		for _, child := range [4][3][3]float32{{vertices[0], ab, ac}, {vertices[1], bc, ab}, {vertices[2], bc, ac}, {ab, bc, ac}} {
    202 			water.subdivide(child, kind, clipOffset)
    203 		}
    204 		return
    205 	}
    206 	water.addVolume(vertices, kind, clipOffset)
    207 }
    208 
    209 func (water *sceneWater) addVolume(vertices [3][3]float32, kind uint32, clipOffset float32) {
    210 	// Source: client/game/bbr.dll/lifted-0017.c, sub_25054050/250527E0.
    211 	if vertices[0] == vertices[1] || vertices[0] == vertices[2] || vertices[1] == vertices[2] {
    212 		return
    213 	}
    214 	sumX := float64(vertices[0][0]) + float64(vertices[1][0]) + float64(vertices[2][0])
    215 	sumZ := float64(vertices[0][2]) + float64(vertices[1][2]) + float64(vertices[2][2])
    216 	bucket := int(sumX*float64(float32(1.0/3))*float64(float32(.01))) - 60*int(sumZ*float64(float32(1.0/3))*float64(float32(-.01)))
    217 	if bucket < 0 || bucket >= 3600 {
    218 		return
    219 	}
    220 	var first, second [3]float32
    221 	for axis := range 3 {
    222 		first[axis] = vertices[1][axis] - vertices[0][axis]
    223 		second[axis] = vertices[2][axis] - vertices[0][axis]
    224 	}
    225 	normal := surfaceCross(first, second)
    226 	if normal[1] < 0 {
    227 		vertices[1], vertices[2] = vertices[2], vertices[1]
    228 		normal = surfaceCross(second, first)
    229 	}
    230 	if math.Abs(float64(normal[0])) < float64(float32(.00001)) && math.Abs(float64(normal[1])) < float64(float32(.00001)) && math.Abs(float64(normal[2])) < float64(float32(.00001)) {
    231 		return
    232 	}
    233 	length := math.Sqrt(surfaceDot(normal, normal))
    234 	for axis := range normal {
    235 		normal[axis] = float32(float64(normal[axis]) / length)
    236 	}
    237 	volume := waterVolume{vertices: vertices, normal: normal, kind: kind - kind&1, clipOffset: clipOffset}
    238 	volume.plane = float32(-(float64(normal[2])*float64(vertices[0][2]) + float64(normal[1])*float64(vertices[0][1]) + float64(normal[0])*float64(vertices[0][0])))
    239 	for axis := range 3 {
    240 		volume.bounds[axis] = min(vertices[0][axis], vertices[1][axis], vertices[2][axis])
    241 		volume.bounds[axis+3] = max(vertices[0][axis], vertices[1][axis], vertices[2][axis])
    242 	}
    243 	water.volumes = append(water.volumes, volume)
    244 }
    245 
    246 func (water *sceneWater) assignZones(ground *terrain, zoneSize uint32) {
    247 	// Source: client/game/kkr.dll/lifted-0061.c, sub_360D0040.
    248 	step := float32(zoneSize) * ground.spacing
    249 	upperX, upperZ := float32(ground.boundsWidth)*ground.spacing-.1, float32(ground.boundsHeight)*ground.spacing-.1
    250 	for index := range water.volumes {
    251 		volume := &water.volumes[index]
    252 		var x, z [3]float32
    253 		for vertex := range 3 {
    254 			x[vertex] = max(float32(.1), min(volume.vertices[vertex][0], upperX))
    255 			z[vertex] = max(float32(.1), min(volume.vertices[vertex][2], upperZ))
    256 		}
    257 		firstX, lastX := max(float32(.1), min(min(x[0], x[1], x[2])-step, upperX)), max(float32(.1), min(max(x[0], x[1], x[2])+step, upperX))
    258 		firstZ, lastZ := max(float32(.1), min(min(z[0], z[1], z[2])-step, upperZ)), max(float32(.1), min(max(z[0], z[1], z[2])+step, upperZ))
    259 		for x := firstX; x < lastX; x += step {
    260 			for z := firstZ; z < lastZ; z += step {
    261 				cell := int(float32(int(z/ground.spacing))/float32(zoneSize))*(ground.boundsWidth/int(zoneSize)) + int(x/ground.spacing/float32(zoneSize))
    262 				water.zones[cell].volumes = append(water.zones[cell].volumes, index)
    263 				water.zones[cell].maxHeight = max(water.zones[cell].maxHeight, volume.bounds[4])
    264 			}
    265 		}
    266 	}
    267 	for _, zone := range water.zones {
    268 		water.maxHeight = max(water.maxHeight, zone.maxHeight)
    269 	}
    270 }
    271 
    272 func (volume *waterVolume) height(origin [3]float32) float64 {
    273 	// Source: client/game/bbr.dll/lifted-0017.c, sub_250529B0.
    274 	if !(origin[1] < volume.bounds[4] && origin[0] > volume.bounds[0] && origin[0] < volume.bounds[3] && origin[2] > volume.bounds[2] && origin[2] < volume.bounds[5]) {
    275 		return 0
    276 	}
    277 	for edge, vertex := range volume.vertices {
    278 		next := volume.vertices[(edge+1)%3]
    279 		dx, dz := next[0]-vertex[0], next[2]-vertex[2]
    280 		side := (float64(origin[2])-float64(vertex[2]))*float64(dx) - (float64(origin[0])-float64(vertex[0]))*float64(dz)
    281 		if !(side < 0) {
    282 			return 0
    283 		}
    284 	}
    285 	height := (-float64(origin[0])*float64(volume.normal[0]) - float64(origin[2])*float64(volume.normal[2]) - float64(volume.plane)) / float64(volume.normal[1])
    286 	if !(height > 0) {
    287 		return 0
    288 	}
    289 	return height
    290 }
    291 
    292 func (water *sceneWater) surface(cell uint32, origin [3]float32, heightLimit float32) waterContact {
    293 	// Sources: client/game/llr.dll/lifted-0017.c, sub_3802D2B0;
    294 	// client/game/llr.dll/lifted-0024.c, sub_3803F0E0.
    295 	if origin[1] > water.maxHeight {
    296 		return waterContact{height: -9999}
    297 	}
    298 	zone := &water.zones[cell]
    299 	if origin[1] < zone.maxHeight {
    300 		for _, index := range zone.volumes {
    301 			volume := &water.volumes[index]
    302 			height := volume.height(origin)
    303 			if height > 0 {
    304 				if height-float64(volume.clipOffset) <= float64(heightLimit) {
    305 					return waterContact{height: float32(height), normal: volume.normal, kind: volume.kind}
    306 				}
    307 				break
    308 			}
    309 		}
    310 	}
    311 	return waterContact{height: -100000}
    312 }