aorecomp

Anarchy Online classic in the browser

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


server/mission_scene.go (10232B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"math"
      6 )
      7 
      8 type missionScene struct {
      9 	ground        *dungeonTilemap
     10 	rooms         []placedRoom
     11 	links         []roomLink
     12 	entrance      *placedRoom
     13 	exitPosition  [3]float32
     14 	exitDirection byte
     15 }
     16 
     17 type roomLink struct {
     18 	rooms     [2]*placedRoom
     19 	position  [3]float32
     20 	direction byte
     21 }
     22 
     23 type placedRoom struct {
     24 	template *dungeonRoom
     25 	position [3]float32
     26 	rotation byte
     27 	surface  *sceneSurface
     28 }
     29 
     30 func roomRotate(point [3]float32, turns byte) [3]float32 {
     31 	switch turns & 3 {
     32 	case 1:
     33 		return [3]float32{point[2], point[1], -point[0]}
     34 	case 2:
     35 		return [3]float32{-point[0], point[1], -point[2]}
     36 	case 3:
     37 		return [3]float32{-point[2], point[1], point[0]}
     38 	}
     39 	return point
     40 }
     41 
     42 func (geometry *sceneGeometry) missionScene(layout missionLayout) (*missionScene, error) {
     43 	if err := layout.validate(); err != nil {
     44 		return nil, err
     45 	}
     46 	if layout.template > math.MaxUint16 {
     47 		return nil, fmt.Errorf("dungeon library identity exceeds surface namespace")
     48 	}
     49 	library, err := geometry.dungeonLibrary(layout.template)
     50 	if err != nil {
     51 		return nil, err
     52 	}
     53 	// Source: client/game/kkr.dll/lifted-0071.c, sub_360F3B20.
     54 	// Generated missions clear unavailable heights before room admission.
     55 	for index, height := range library.ground.tilesetHeights {
     56 		if height >= 6553.5 {
     57 			library.ground.tilesetHeights[index] = 0
     58 		}
     59 	}
     60 	scene := &missionScene{ground: library.ground, rooms: make([]placedRoom, len(layout.rooms))}
     61 	minimum := layout.rooms[0].floor
     62 	for _, room := range layout.rooms {
     63 		minimum = min(minimum, room.floor)
     64 	}
     65 	for index, placement := range layout.rooms {
     66 		if int(placement.template) >= len(library.rooms) {
     67 			return nil, fmt.Errorf("unknown dungeon room %d", placement.template)
     68 		}
     69 		room := &scene.rooms[index]
     70 		room.template, room.rotation = &library.rooms[placement.template], placement.rotation
     71 		if room.template.water {
     72 			return nil, fmt.Errorf("dungeon room %d requires liquid physics", placement.template)
     73 		}
     74 		width := int(room.template.bounds[2] - room.template.bounds[0])
     75 		depth := int(room.template.bounds[3] - room.template.bounds[1])
     76 		if width%5 != 0 || depth%5 != 0 {
     77 			return nil, fmt.Errorf("dungeon room dimensions are not multiples of five")
     78 		}
     79 		spacing := library.ground.spacing
     80 		rotatedWidth, rotatedDepth := width, depth
     81 		if room.rotation&1 != 0 {
     82 			rotatedWidth, rotatedDepth = depth, width
     83 		}
     84 		x := float32(placement.x) * 10
     85 		z := float32(layout.depth)*10 - float32(placement.z)*10 - float32(rotatedDepth)*spacing
     86 		if z < 0 || x+float32(rotatedWidth)*spacing > float32(layout.width)*10 {
     87 			return nil, fmt.Errorf("dungeon room %d lies outside building", index)
     88 		}
     89 
     90 		// KKR CreateDungeonRoom 0x100f5540/0x100f5e10: rotate the room anchor.
     91 		anchor := [3]float32{float32(((width-1)&^1)+1)*.5*spacing + 1, 0, float32(((depth-1)&^1)+1)*.5*spacing + 1}
     92 		anchor = roomRotate(anchor, room.rotation)
     93 		switch room.rotation {
     94 		case 1:
     95 			anchor[2] += float32(width) * spacing
     96 		case 2:
     97 			anchor[0] += float32(width) * spacing
     98 			anchor[2] += float32(depth) * spacing
     99 		case 3:
    100 			anchor[0] += float32(depth) * spacing
    101 		}
    102 		room.position = [3]float32{x + anchor[0], float32(int(placement.floor)-int(minimum))*float32(layout.floorHeight) + room.template.position[1], z + anchor[2]}
    103 		room.surface = geometry.surface(layout.template<<16 | uint32(placement.template))
    104 		if room.surface == nil {
    105 			return nil, fmt.Errorf("missing dungeon room surface %d", placement.template)
    106 		}
    107 	}
    108 	return scene, nil
    109 }
    110 
    111 func (room *placedRoom) templatePoint(point [3]float32) [3]float32 {
    112 	// LLR n3Room_t::CalcTemplatePosFromGlobalPos 0x10033320.
    113 	for axis := range point {
    114 		point[axis] -= room.position[axis]
    115 	}
    116 	point = roomRotate(point, room.template.rotation-room.rotation)
    117 	for axis := range point {
    118 		point[axis] += room.template.position[axis]
    119 	}
    120 	return point
    121 }
    122 
    123 func (room *placedRoom) globalPoint(point [3]float32) [3]float32 {
    124 	// LLR n3Room_t::CalcGlobalFromTemplatePos 0x100333f0.
    125 	for axis := range point {
    126 		point[axis] -= room.template.position[axis]
    127 	}
    128 	point = roomRotate(point, room.rotation-room.template.rotation)
    129 	for axis := range point {
    130 		point[axis] += room.position[axis]
    131 	}
    132 	return point
    133 }
    134 
    135 func (scene *missionScene) roomAt(point [3]float32) *placedRoom {
    136 	// LLR GetTilemapIndexFromPos 0x100326e0 / IsPosInside 0x10031910.
    137 	for index := range scene.rooms {
    138 		room := &scene.rooms[index]
    139 		local := room.tilePoint(point, scene.ground.spacing)
    140 		width := int(room.template.bounds[2] - room.template.bounds[0])
    141 		depth := int(room.template.bounds[3] - room.template.bounds[1])
    142 		x := int(math.Floor(float64(local[0] / scene.ground.spacing)))
    143 		z := int(math.Floor(float64(local[2] / scene.ground.spacing)))
    144 		if x < 0 || x >= width || z < 0 || z >= depth {
    145 			continue
    146 		}
    147 		x, z = x+int(room.template.bounds[0]), z+int(room.template.bounds[1])
    148 		cell := z*scene.ground.width + x
    149 		kind := scene.ground.tiles[cell] & 127
    150 		if kind == 0 {
    151 			continue
    152 		}
    153 		floor := min(scene.ground.floors[cell], scene.ground.floors[z*scene.ground.width+max(x-1, 0)],
    154 			scene.ground.floors[max(z-1, 0)*scene.ground.width+x], scene.ground.floors[max(z-1, 0)*scene.ground.width+max(x-1, 0)])
    155 		height := float32(floor)*scene.ground.scale - room.template.minHeight + room.position[1]
    156 		ceiling := (float32(int(scene.ground.ceilings[cell])-32)*scene.ground.scale*.25+1)*scene.ground.tilesetHeights[kind] + height
    157 		if point[1] >= height-.5 && point[1] <= ceiling+.5 {
    158 			return room
    159 		}
    160 	}
    161 	return nil
    162 }
    163 
    164 func (room *placedRoom) tilePoint(point [3]float32, spacing float32) [3]float32 {
    165 	// Grid lookup rotates by the placed orientation, independently of mesh orientation.
    166 	for axis := range point {
    167 		point[axis] -= room.position[axis]
    168 	}
    169 	point = roomRotate(point, -room.rotation)
    170 	width := int(room.template.bounds[2] - room.template.bounds[0])
    171 	depth := int(room.template.bounds[3] - room.template.bounds[1])
    172 	point[0] += float32(((width-1)&^1)+1)*.5*spacing + 1
    173 	point[2] += float32(((depth-1)&^1)+1)*.5*spacing + 1
    174 	return point
    175 }
    176 
    177 func (scene *missionScene) sphere(center [3]float32, radius float32) surfaceContact {
    178 	// LLR n3RoomSurface_t::GetSphereIntersection 0x10036780.
    179 	room := scene.roomAt(center)
    180 	if room == nil {
    181 		return surfaceContact{}
    182 	}
    183 	contact := room.surface.sphere(room.templatePoint(center), radius)
    184 	if contact.hit {
    185 		contact.point = room.globalPoint(contact.point)
    186 		contact.normal = roomRotate(contact.normal, room.rotation-room.template.rotation)
    187 	}
    188 	return contact
    189 }
    190 
    191 func (scene *missionScene) floor(origin [3]float32, point, normal *[3]float32) {
    192 	// LLR n3RoomSurface_t::CalculateClosestPoint 0x10035a90.
    193 	room := scene.roomAt(origin)
    194 	if room == nil {
    195 		*point = [3]float32{origin[0], 0, origin[2]}
    196 		return
    197 	}
    198 	template := room.templatePoint(origin)
    199 	// The KD-tree probe uses the initialized upward normal as its one-unit bound.
    200 	static := surfaceIntersection{point: [3]float32{template[0], 0, template[2]}, normal: [3]float32{0, 1, 0}}
    201 	if room.surface.primary != nil {
    202 		static = room.surface.primary.floor(template, 1)
    203 	}
    204 	// The native room wrapper transforms the mesh point and retains its returned normal.
    205 	*point, *normal = room.globalPoint(static.point), static.normal
    206 
    207 	// Dungeon floor vertices straddle the selected tile; every cell uses the same diagonal.
    208 	atlas := room.tilePoint(origin, scene.ground.spacing)
    209 	atlas[0] += float32(room.template.bounds[0]) * scene.ground.spacing
    210 	atlas[2] += float32(room.template.bounds[1]) * scene.ground.spacing
    211 	height, slope := scene.ground.floor(atlas)
    212 	height = height - room.template.minHeight + room.position[1]
    213 	if height > point[1] {
    214 		*point = [3]float32{origin[0], height, origin[2]}
    215 		*normal = roomRotate(slope, room.rotation)
    216 	}
    217 	point[1] = max(float32(.001), min(point[1], float32(1999.9)))
    218 }
    219 
    220 func (ground *dungeonTilemap) floor(origin [3]float32) (float32, [3]float32) {
    221 	// client/game/llr.dll/lifted-0021.c, sub_380385E0: GetHeightAndNormalAtPos, dungeon diagonal.
    222 	x, z := int(math.Floor(float64(origin[0]/ground.spacing))), int(math.Floor(float64(origin[2]/ground.spacing)))
    223 	u, v := origin[0]/ground.spacing-float32(x), origin[2]/ground.spacing-float32(z)
    224 	var corners [4][3]float32
    225 	for index, offset := range [4][2]int{{-1, -1}, {0, -1}, {0, 0}, {-1, 0}} {
    226 		cell := max(z+offset[1], 0)*ground.width + max(x+offset[0], 0)
    227 		corners[index] = [3]float32{float32(offset[0]+1) * ground.spacing,
    228 			float32(ground.floors[cell]) * ground.scale,
    229 			float32(offset[1]+1) * ground.spacing}
    230 	}
    231 	indices := [3]int{0, 1, 2}
    232 	height := corners[0][1] + u*(corners[1][1]-corners[0][1]) + v*(corners[2][1]-corners[1][1])
    233 	if u <= v {
    234 		indices = [3]int{0, 2, 3}
    235 		height = corners[0][1] + u*(corners[2][1]-corners[3][1]) + v*(corners[3][1]-corners[0][1])
    236 	}
    237 	var first, second [3]float32
    238 	for axis := range 3 {
    239 		first[axis] = corners[indices[1]][axis] - corners[indices[0]][axis]
    240 		second[axis] = corners[indices[2]][axis] - corners[indices[0]][axis]
    241 	}
    242 	return height, surfaceNormal(second, first)
    243 }
    244 
    245 func (scene *missionScene) line(origin, end [3]float32) surfaceIntersection {
    246 	// LLR n3RoomSurface_t::GetLineIntersection 0x100364c0 selects one endpoint room.
    247 	room := scene.roomAt(origin)
    248 	if room == nil {
    249 		room = scene.roomAt(end)
    250 	}
    251 	if room == nil {
    252 		return surfaceIntersection{}
    253 	}
    254 	contact := room.surface.line(room.templatePoint(origin), room.templatePoint(end))
    255 	if contact.hit {
    256 		contact.point = room.globalPoint(contact.point)
    257 		contact.normal = roomRotate(contact.normal, room.rotation-room.template.rotation)
    258 		return contact
    259 	}
    260 
    261 	// A mesh miss falls back to the floor plane sampled at the original start point.
    262 	var point, normal [3]float32
    263 	scene.floor(origin, &point, &normal)
    264 	delta := [3]float32{end[0] - origin[0], end[1] - origin[1], end[2] - origin[2]}
    265 	denominator := surfaceDot(delta, normal)
    266 	if denominator == 0 {
    267 		return surfaceIntersection{}
    268 	}
    269 	fraction := float32(surfaceDot([3]float32{point[0] - origin[0], point[1] - origin[1], point[2] - origin[2]}, normal) / denominator)
    270 	if !(fraction >= 0 && fraction < 1) {
    271 		return surfaceIntersection{}
    272 	}
    273 	for axis := range point {
    274 		point[axis] = origin[axis] + delta[axis]*fraction
    275 	}
    276 	return surfaceIntersection{point: point, normal: normal, fraction: fraction, hit: true}
    277 }