aorecomp

Anarchy Online classic in the browser

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


server/surface_line.go (8109B)


      1 package main
      2 
      3 type surfaceIntersection struct {
      4 	point, normal [3]float32
      5 	fraction      float32
      6 	hit           bool
      7 }
      8 
      9 func (group *surfaceGroup) line(origin, delta [3]float32) surfaceIntersection {
     10 	// Source: client/game/llr.dll/lifted-0028.c, sub_380475F0/38047800.
     11 	best := surfaceIntersection{fraction: 1}
     12 	for index := range group.geometry {
     13 		triangle := &group.geometry[index]
     14 		denominator := float64(delta[2])*float64(triangle.normal[2]) + float64(delta[0])*float64(triangle.normal[0]) + float64(delta[1])*float64(triangle.normal[1])
     15 		if !(denominator < 0) {
     16 			continue
     17 		}
     18 		dot := float64(origin[1])*float64(triangle.normal[1]) + float64(origin[2])*float64(triangle.normal[2]) + float64(origin[0])*float64(triangle.normal[0])
     19 		fraction := float32((-float64(triangle.plane) - dot) / denominator)
     20 		if float64(fraction)+float64(float32(.0000011920928955078125)) < 0 || float64(best.fraction)+float64(float32(.0000011920928955078125)) < float64(fraction) {
     21 			continue
     22 		}
     23 		var point [3]float32
     24 		for axis := range 3 {
     25 			product := float32(delta[axis] * fraction)
     26 			point[axis] = origin[axis] + product
     27 		}
     28 		inside := true
     29 		for edge := range 3 {
     30 			if surfaceDot(point, triangle.sides[edge])+float64(triangle.side[edge]) > 0 {
     31 				inside = false
     32 				break
     33 			}
     34 		}
     35 		if inside && fraction < best.fraction {
     36 			best = surfaceIntersection{point: point, normal: triangle.normal, fraction: fraction, hit: true}
     37 		}
     38 	}
     39 	return best
     40 }
     41 
     42 func surfaceLineExcluded(bounds [6]float32, origin, end [3]float32) bool {
     43 	// Source: client/game/llr.dll/lifted-0028.c, sub_380457B0.
     44 	for _, point := range [2][3]float32{origin, end} {
     45 		inside := true
     46 		for axis := range 3 {
     47 			if point[axis] < bounds[axis] || point[axis] > bounds[axis+3] {
     48 				inside = false
     49 			}
     50 		}
     51 		if inside {
     52 			return false
     53 		}
     54 	}
     55 	for _, axis := range [3]int{1, 0, 2} {
     56 		if origin[axis] == end[axis] {
     57 			continue
     58 		}
     59 		for _, plane := range [2]float32{bounds[axis], bounds[axis+3]} {
     60 			if origin[axis] < plane && end[axis] < plane || origin[axis] > plane && end[axis] > plane {
     61 				continue
     62 			}
     63 			fraction := float32((float64(plane) - float64(origin[axis])) / (float64(end[axis]) - float64(origin[axis])))
     64 			inside := true
     65 			for other := range 3 {
     66 				if other == axis {
     67 					continue
     68 				}
     69 				coordinate := (float64(end[other])-float64(origin[other]))*float64(fraction) + float64(origin[other])
     70 				if coordinate < float64(bounds[other]) || coordinate > float64(bounds[other+3]) {
     71 					inside = false
     72 				}
     73 			}
     74 			if inside {
     75 				return false
     76 			}
     77 		}
     78 	}
     79 	return true
     80 }
     81 
     82 func surfaceSegmentOutside(bounds [6]float32, origin, end [3]float32) bool {
     83 	for axis := range 3 {
     84 		if origin[axis] < bounds[axis] && end[axis] < bounds[axis] || origin[axis] > bounds[axis+3] && end[axis] > bounds[axis+3] {
     85 			return true
     86 		}
     87 	}
     88 	return false
     89 }
     90 
     91 func (surface *sceneSurface) line(origin, end [3]float32) surfaceIntersection {
     92 	// Source: client/game/llr.dll/lifted-0021.c, sub_38037B50.
     93 	if surface.primary == nil {
     94 		return surfaceIntersection{}
     95 	}
     96 	invisible := surfaceIntersection{}
     97 	if surface.invisible != nil {
     98 		invisible = surface.invisible.line(origin, end)
     99 	}
    100 	primary := surface.primary.line(origin, end)
    101 	if !invisible.hit || primary.hit && surfaceDistanceSquared(origin, primary.point) < surfaceDistanceSquared(origin, invisible.point) {
    102 		return primary
    103 	}
    104 	return invisible
    105 }
    106 
    107 func (tree *surfaceTree) line(origin, end [3]float32) surfaceIntersection {
    108 	tree.initialize()
    109 	return tree.nodeLine(0, origin, end, 1)
    110 }
    111 
    112 func (tree *surfaceTree) nodeLine(index int, origin, end [3]float32, bound float32) surfaceIntersection {
    113 	// Source: client/game/llr.dll/lifted-0027.c, sub_38044C90.
    114 	delta := [3]float32{end[0] - origin[0], end[1] - origin[1], end[2] - origin[2]}
    115 	var clipped [3]float32
    116 	for axis := range 3 {
    117 		product := float32(delta[axis] * bound)
    118 		clipped[axis] = origin[axis] + product
    119 	}
    120 	clipped[0] = float32(float64(origin[0]) + float64(delta[0])*float64(bound))
    121 	found := surfaceIntersection{}
    122 	node := &tree.nodes[index]
    123 	for len(node.groups) == 0 {
    124 		if float32(surfaceDistanceSquared(clipped, origin)) > 3 {
    125 			if surfaceLineExcluded(node.bounds, origin, clipped) {
    126 				return found
    127 			}
    128 		} else if surfaceSegmentOutside(node.bounds, origin, clipped) {
    129 			return found
    130 		}
    131 		low := origin[node.axis] < node.split || clipped[node.axis] < node.split
    132 		high := origin[node.axis] >= node.split || clipped[node.axis] >= node.split
    133 		if !low {
    134 			node = &tree.nodes[node.children[1]]
    135 			continue
    136 		}
    137 		if !high {
    138 			node = &tree.nodes[node.children[0]]
    139 			continue
    140 		}
    141 		first, next := node.children[0], node.children[1]
    142 		upper := origin[node.axis] >= node.split
    143 		if upper {
    144 			first, next = next, first
    145 		}
    146 		candidate := tree.nodeLine(first, origin, end, bound)
    147 		if candidate.hit && candidate.fraction <= bound {
    148 			for axis := range 3 {
    149 				product := float32(delta[axis] * candidate.fraction)
    150 				clipped[axis] = origin[axis] + product
    151 			}
    152 			if upper && clipped[node.axis] >= node.split || !upper && clipped[node.axis] < node.split {
    153 				return candidate
    154 			}
    155 			found, bound = candidate, candidate.fraction
    156 		}
    157 		node = &tree.nodes[next]
    158 	}
    159 	if len(node.groups) != 1 && surfaceLineExcluded(node.bounds, origin, end) {
    160 		return found
    161 	}
    162 	for _, index := range node.groups {
    163 		group := &tree.groups[index]
    164 		if surfaceLineExcluded(group.bounds, origin, clipped) {
    165 			continue
    166 		}
    167 		candidate := group.line(origin, delta)
    168 		if candidate.hit && candidate.fraction <= bound {
    169 			found, bound = candidate, candidate.fraction
    170 		}
    171 	}
    172 	return found
    173 }
    174 
    175 func surfaceFloorOverlap(bounds [6]float32, origin [3]float32, displacement float32) bool {
    176 	if origin[0] < bounds[0] || origin[0] > bounds[3] || origin[2] < bounds[2] || origin[2] > bounds[5] {
    177 		return false
    178 	}
    179 	end := float64(origin[1]) + float64(displacement)
    180 	return !(float64(origin[1]) < float64(bounds[1]) && end < float64(bounds[1]) || float64(origin[1]) > float64(bounds[4]) && end > float64(bounds[4]))
    181 }
    182 
    183 func (tree *surfaceTree) floor(origin [3]float32, bound float32) surfaceIntersection {
    184 	// Source: client/game/llr.dll/lifted-0028.c, sub_380460F0.
    185 	tree.initialize()
    186 	result := tree.nodeFloor(0, origin, bound)
    187 	if !result.hit {
    188 		result.point = [3]float32{origin[0], 0, origin[2]}
    189 		result.normal = [3]float32{0, 1, 0}
    190 	}
    191 	return result
    192 }
    193 
    194 func (tree *surfaceTree) nodeFloor(index int, origin [3]float32, bound float32) surfaceIntersection {
    195 	// Source: client/game/llr.dll/lifted-0028.c, sub_38045BC0.
    196 	// The native recursion carries a mixed displacement/fraction scalar; retain it literally.
    197 	displacement := -bound
    198 	found := surfaceIntersection{}
    199 	node := &tree.nodes[index]
    200 	for len(node.groups) == 0 {
    201 		if !surfaceFloorOverlap(node.bounds, origin, displacement) {
    202 			return found
    203 		}
    204 		low, high := origin[node.axis] < node.split, origin[node.axis] >= node.split
    205 		if node.axis == 1 {
    206 			low = low || float64(origin[1])+float64(displacement) < float64(node.split)
    207 			high = high || float64(origin[1])+float64(displacement) >= float64(node.split)
    208 		}
    209 		if low && high {
    210 			first, next := node.children[0], node.children[1]
    211 			upper := origin[node.axis] >= node.split
    212 			if upper {
    213 				first, next = next, first
    214 			}
    215 			candidate := tree.nodeFloor(first, origin, bound)
    216 			if candidate.hit && candidate.fraction <= bound {
    217 				end := float64(origin[1]) + float64(displacement)
    218 				if upper && end >= float64(node.split) || !upper && end < float64(node.split) {
    219 					return candidate
    220 				}
    221 				found, bound = candidate, candidate.fraction
    222 			}
    223 			node = &tree.nodes[next]
    224 		} else if low {
    225 			node = &tree.nodes[node.children[0]]
    226 		} else {
    227 			node = &tree.nodes[node.children[1]]
    228 		}
    229 	}
    230 	if len(node.groups) != 1 && !surfaceFloorOverlap(node.bounds, origin, displacement) {
    231 		return found
    232 	}
    233 	for _, index := range node.groups {
    234 		group := &tree.groups[index]
    235 		if !surfaceFloorOverlap(group.bounds, origin, displacement) {
    236 			continue
    237 		}
    238 		candidate := group.line(origin, [3]float32{0, displacement, 0})
    239 		if candidate.hit && candidate.fraction <= bound {
    240 			found, bound = candidate, candidate.fraction
    241 		}
    242 	}
    243 	return found
    244 }