aorecomp

Anarchy Online classic in the browser

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


server/terrain_line.go (3995B)


      1 package main
      2 
      3 import "math"
      4 
      5 type gridLine struct {
      6 	cell, step     [2]int
      7 	next, interval [2]float32
      8 	remaining      int
      9 }
     10 
     11 func newGridLine(start, delta [2]float64, end [2]int) gridLine {
     12 	// Sources: client/game/qqr.dll/lifted-0005.c, sub_4200F990;
     13 	// client/game/llr.dll/lifted-0023.c, sub_3803B970. Callers preserve their own spills.
     14 	line := gridLine{cell: [2]int{int(start[0]), int(start[1])}, remaining: 1}
     15 	for axis := range 2 {
     16 		distance := end[axis] - line.cell[axis]
     17 		if distance < 0 {
     18 			distance = -distance
     19 		}
     20 		line.remaining += distance
     21 		line.step[axis] = 1
     22 		fraction := math.Floor(start[axis]) + 1 - start[axis]
     23 		if delta[axis] < 0 {
     24 			line.step[axis] = -1
     25 			fraction = start[axis] - math.Floor(start[axis])
     26 		}
     27 		line.interval[axis] = float32(float64(line.step[axis]*100) / delta[axis])
     28 		line.next[axis] = float32(fraction * float64(line.interval[axis]))
     29 	}
     30 	return line
     31 }
     32 
     33 func (line *gridLine) advance() {
     34 	line.remaining--
     35 	if line.remaining == 0 {
     36 		return
     37 	}
     38 	axis := 0
     39 	if line.next[0] >= line.next[1] {
     40 		axis = 1
     41 	}
     42 	line.cell[axis] += line.step[axis]
     43 	line.next[axis] += line.interval[axis]
     44 }
     45 
     46 func (ground *terrain) line(origin, end, delta [3]float32) surfaceIntersection {
     47 	// Source: client/game/llr.dll/lifted-0023.c, sub_3803B970.
     48 	start := [2]float64{float64(origin[0] / ground.spacing), float64(origin[2] / ground.spacing)}
     49 	endX, endZ := float64(end[0])/float64(ground.spacing), end[2]/ground.spacing
     50 	travel := [2]float64{endX - start[0], float64(float32(endZ - float32(start[1])))}
     51 	for cells := newGridLine(start, travel, [2]int{int(endX), int(endZ)}); cells.remaining > 0; cells.advance() {
     52 		column, row := cells.cell[0], cells.cell[1]
     53 		if column < 0 || column >= ground.boundsWidth || row < 0 || row >= ground.boundsHeight {
     54 			continue
     55 		}
     56 		contact := ground.tileLine(column, row, origin, end, delta)
     57 		if contact.hit && surfaceDistanceSquared(contact.point, origin) < surfaceDistanceSquared(end, origin) {
     58 			return contact
     59 		}
     60 	}
     61 	return surfaceIntersection{}
     62 }
     63 
     64 func (ground *terrain) tileLine(column, row int, origin, end, delta [3]float32) surfaceIntersection {
     65 	// Source: client/game/llr.dll/lifted-0023.c, sub_3803C220/3803C6B0.
     66 	corners := ground.corners(column, row)
     67 	maximum := max(corners[0][1], corners[1][1], corners[2][1], corners[3][1])
     68 	if origin[1] > maximum && end[1] > maximum {
     69 		return surfaceIntersection{}
     70 	}
     71 	triangles := [2][3]int{{0, 1, 2}, {2, 3, 0}}
     72 	if (column+row)&1 != 0 {
     73 		triangles = [2][3]int{{0, 1, 3}, {1, 2, 3}}
     74 	}
     75 	for index, vertices := range triangles {
     76 		var first, second, toOrigin [3]float32
     77 		for axis := range 3 {
     78 			first[axis] = corners[vertices[1]][axis] - corners[vertices[0]][axis]
     79 			second[axis] = corners[vertices[2]][axis] - corners[vertices[0]][axis]
     80 			toOrigin[axis] = corners[vertices[0]][axis] - origin[axis]
     81 		}
     82 		normal := surfaceCross(first, second)
     83 		denominator := surfaceDot(delta, normal)
     84 		if !(denominator > 0) {
     85 			continue
     86 		}
     87 		fraction := surfaceDot(toOrigin, normal) / float64(float32(denominator))
     88 		if (column+row)&1 == 0 && index == 0 {
     89 			fraction = float64(float32(fraction))
     90 		}
     91 		var point [3]float32
     92 		for axis := range 3 {
     93 			product := float32(float64(delta[axis]) * fraction)
     94 			point[axis] = origin[axis] + product
     95 		}
     96 		var fromFirst, fromSecond, fromThird, lastEdge [3]float32
     97 		for axis := range 3 {
     98 			fromFirst[axis] = point[axis] - corners[vertices[0]][axis]
     99 			fromSecond[axis] = point[axis] - corners[vertices[1]][axis]
    100 			fromThird[axis] = point[axis] - corners[vertices[2]][axis]
    101 			lastEdge[axis] = corners[vertices[2]][axis] - corners[vertices[1]][axis]
    102 		}
    103 		cross := surfaceCross(first, fromFirst)
    104 		if surfaceDot(cross, surfaceCross(lastEdge, fromSecond)) < float64(float32(-.0005)) || surfaceDot(cross, surfaceCross(fromThird, second)) < float64(float32(-.0005)) {
    105 			continue
    106 		}
    107 		return surfaceIntersection{point: point, normal: surfaceNormal(second, first), fraction: float32(fraction), hit: true}
    108 	}
    109 	return surfaceIntersection{}
    110 }