aorecomp

Anarchy Online classic in the browser

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


server/scene_queries.go (3936B)


      1 package main
      2 
      3 func (layout scenePlayfield) clip(origin, end *[3]float32) bool {
      4 	// Source: client/game/llr.dll/lifted-0023.c, sub_3803B970.
      5 	code := func(point [3]float32) int {
      6 		result := 0
      7 		if point[2] >= layout.sizeZ {
      8 			result |= 8
      9 		}
     10 		if point[2] < 0 {
     11 			result |= 4
     12 		}
     13 		if point[0] >= layout.sizeX {
     14 			result |= 2
     15 		}
     16 		if point[0] < 0 {
     17 			result |= 1
     18 		}
     19 		return result
     20 	}
     21 	for {
     22 		first, last := code(*origin), code(*end)
     23 		if first|last == 0 {
     24 			return true
     25 		}
     26 		if first&last != 0 {
     27 			return false
     28 		}
     29 		outside := first
     30 		if outside == 0 {
     31 			outside = last
     32 		}
     33 		axis, boundary, target := 2, float32(0), float32(0)
     34 		if outside&8 != 0 {
     35 			boundary, target = layout.sizeZ, layout.sizeZ-.1
     36 		} else if outside&4 == 0 {
     37 			axis = 0
     38 			if outside&2 != 0 {
     39 				boundary, target = layout.sizeX, layout.sizeX-.1
     40 			}
     41 		}
     42 		fraction := (float64(boundary) - float64(origin[axis])) / (float64(end[axis]) - float64(origin[axis]))
     43 		var point [3]float32
     44 		for other := range 3 {
     45 			point[other] = float32((float64(end[other])-float64(origin[other]))*fraction + float64(origin[other]))
     46 		}
     47 		point[axis] = target
     48 		if first != 0 {
     49 			*origin = point
     50 		} else {
     51 			*end = point
     52 		}
     53 	}
     54 }
     55 
     56 func (layout scenePlayfield) veto(position, previous *[3]float32) bool {
     57 	// Source: client/game/llr.dll/lifted-0023.c, sub_3803CB70.
     58 	if position[0] >= 0 && position[0] < layout.sizeX && position[2] >= 0 && position[2] < layout.sizeZ && position[1] >= 0 && position[1] < 2000 {
     59 		return false
     60 	}
     61 	if previous != nil {
     62 		*position = *previous
     63 	}
     64 	position[0] = max(float32(.1), min(position[0], layout.sizeX-.1))
     65 	position[2] = max(float32(.1), min(position[2], layout.sizeZ-.1))
     66 	position[1] = max(float32(.01), min(position[1], float32(1999.9)))
     67 	return true
     68 }
     69 
     70 func (geometry *sceneGeometry) line(playfield uint32, ground *terrain, origin, end [3]float32, clip bool) surfaceIntersection {
     71 	if scene := geometry.dungeon(playfield); scene != nil {
     72 		return scene.line(origin, end)
     73 	}
     74 	layout, exists := geometry.playfields[playfield]
     75 	if !exists {
     76 		return surfaceIntersection{}
     77 	}
     78 	// Native terrain keeps the pre-clipping displacement while cells use clipped endpoints.
     79 	delta := [3]float32{end[0] - origin[0], end[1] - origin[1], end[2] - origin[2]}
     80 	if clip && !layout.clip(&origin, &end) {
     81 		return surfaceIntersection{}
     82 	}
     83 	static := geometry.cellLine(playfield, origin, end)
     84 	terrain := ground.line(origin, end, delta)
     85 	if terrain.hit && (!static.hit || !(surfaceDistanceSquared(origin, static.point) < surfaceDistanceSquared(origin, terrain.point))) {
     86 		return terrain
     87 	}
     88 	return static
     89 }
     90 
     91 func (geometry *sceneGeometry) floor(playfield uint32, ground *terrain, origin [3]float32, point, normal *[3]float32) waterContact {
     92 	if scene := geometry.dungeon(playfield); scene != nil {
     93 		scene.floor(origin, point, normal)
     94 		return waterContact{}
     95 	}
     96 	// Source: client/game/llr.dll/lifted-0023.c, sub_3803B690.
     97 	// Invalid cells leave the caller's point and normal unchanged.
     98 	layout, exists := geometry.playfields[playfield]
     99 	if !exists || ground == nil {
    100 		return waterContact{}
    101 	}
    102 	cell, inside := layout.cellIndex(origin)
    103 	if !inside {
    104 		return waterContact{}
    105 	}
    106 	height, slope, sampled := ground.sample(origin[0], origin[2])
    107 	if sampled {
    108 		*normal = slope
    109 	}
    110 	*point = [3]float32{origin[0], height, origin[2]}
    111 	if above := origin[1] - height; above >= 0 {
    112 		surface := geometry.surface(playfield<<16 | cell)
    113 		if surface != nil {
    114 			static := surfaceIntersection{point: [3]float32{origin[0], 0, origin[2]}, normal: [3]float32{0, 1, 0}}
    115 			if surface.primary != nil {
    116 				static = surface.primary.floor(origin, above+.3)
    117 			}
    118 			if static.point[1] > height {
    119 				*point, *normal = static.point, static.normal
    120 			}
    121 		}
    122 	}
    123 	water := geometry.water(playfield, ground).surface(cell, *point, origin[1])
    124 	if water.height-float32(1.2) > point[1] {
    125 		point[1] = water.height - float32(1.2)
    126 		*normal = water.normal
    127 	}
    128 	return water
    129 }