server/player_collision.go (14166B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "math" 6 ) 7 8 type playerCollisionBody struct { 9 mesh uint32 10 scale float32 11 sphere collisionSphere 12 form uint32 13 base int32 14 bonus int32 15 } 16 17 func (player *character) collisionBody() collisionSphere { 18 if player.body.mesh == 0 { 19 // Setup constructs the normal CIR before render-only fatness changes. 20 player.body.base, player.body.scale = int32(player.baseScale()), 1 21 mesh := player.humanCollisionMesh(1) 22 if template := gamedata.MonsterTemplates[player.monsterData]; template != nil { 23 mesh = template.Stats[12] 24 } 25 player.replaceCollisionMesh(mesh) 26 player.scaleCollisionBody(1) 27 28 // FullCharacter applies implants first, then ordinary equipment in slot order. 29 // Source: client/game/kkr.dll/lifted-0029.c, sub_3605F220/sub_3605F150. 30 for _, slots := range [][2]uint32{{32, 48}, {0, 32}} { 31 for slot := slots[0]; slot < slots[1]; slot++ { 32 for _, item := range player.inventory.Items { 33 if item.Slot == slot && slot != 6 && slot != 8 { 34 player.wearCollisionBody(item, false) 35 } 36 } 37 } 38 } 39 // FullCharacter restores the live base stat list after the worn effects. 40 player.scaleCollisionBody(1) 41 } 42 43 // Base edits and numeric modifiers refresh extent without rebuilding the CAT. 44 base := int32(player.baseScale()) 45 if player.driving() { 46 base = 20 47 } 48 bonus := player.inventory.equipmentBonus(360) + player.nanoBonus(360) 49 if player.body.base != base || player.body.bonus != bonus { 50 player.body.base, player.body.bonus = base, bonus 51 player.scaleCollisionBody(1) 52 } 53 return player.body.sphere 54 } 55 56 func (player *character) humanCollisionMesh(fatness uint16) uint32 { 57 appearance := player.resolvedAppearance() 58 for _, body := range gamedata.PlayerAppearances.Bodies { 59 if body.Breed == appearance.Breed && body.Sex == appearance.Sex { 60 return body.Meshes[fatness] 61 } 62 } 63 panic("missing player collision body") 64 } 65 66 func (player *character) replaceCollisionMesh(mesh uint32) { 67 player.body.mesh, player.body.sphere = mesh, player.geometry.model(mesh) 68 player.body.sphere.radius *= player.body.scale 69 player.body.sphere.offset[1] *= player.body.scale 70 } 71 72 func (player *character) scaleCollisionBody(minimum int32) { 73 scale := float32(max(minimum, player.body.base+player.body.bonus)) * float32(.01) 74 if player.body.scale != scale { 75 // SetBodyScale multiplies the existing primitive instead of decoding it again. 76 ratio := scale / player.body.scale 77 player.body.sphere.radius *= ratio 78 player.body.sphere.offset[1] *= ratio 79 player.body.scale = scale 80 } 81 } 82 83 func (item ownedItem) wearFunctions(yield func(gamedata.ItemFunction, gamedata.ItemFunction) bool) { 84 if !item.fitsEquipmentSlot(item.Slot) { 85 return 86 } 87 low, high := item.templateIDs() 88 first, second := gamedata.ItemTemplates[low], gamedata.ItemTemplates[high] 89 for sectionIndex, section := range first.Sections { 90 if section.Type != 2 || section.Subtype != 14 { 91 continue 92 } 93 for index, function := range *section.Functions { 94 if !yield(function, (*second.Sections[sectionIndex].Functions)[index]) { 95 return 96 } 97 } 98 } 99 } 100 101 func (player *character) wearCollisionBody(item ownedItem, remove bool) { 102 for function, upper := range item.wearFunctions { 103 player.wearCollisionFunction(item, function, upper, remove) 104 } 105 } 106 107 func (player *character) wearEffects(item ownedItem, remove bool) { 108 // Live effects share authored order; reconstructing a body must not move it. 109 for function, upper := range item.wearFunctions { 110 if player.body.mesh != 0 { 111 player.wearCollisionFunction(item, function, upper, remove) 112 } 113 if function.ID == 53138 { 114 player.changeFlight(remove) 115 } 116 } 117 } 118 119 func (player *character) wearCollisionFunction(item ownedItem, function, upper gamedata.ItemFunction, remove bool) { 120 switch function.ID { 121 case 53012, 53045: 122 if *function.Arguments[0].Integer == 360 { 123 low, high := item.templateIDs() 124 value := interpolateItemValue(*function.Arguments[1].Integer, *upper.Arguments[1].Integer, *gamedata.ItemTemplates[low].Quality, *gamedata.ItemTemplates[high].Quality, item.Quality) 125 if remove { 126 value = -value 127 } 128 player.body.bonus += value 129 player.scaleCollisionBody(1) 130 } 131 case 53060: 132 form := uint32(*function.Arguments[0].Integer) 133 if remove && player.body.form == form { 134 player.body.form = 0 135 fatness := player.resolvedAppearance().Fatness 136 if fatness > 2 { 137 fatness = 1 138 } 139 player.replaceCollisionMesh(player.humanCollisionMesh(fatness)) 140 player.scaleCollisionBody(1) 141 } else if !remove && player.body.form == 0 { 142 player.body.form = form 143 mesh := gamedata.MonsterTemplates[form].Stats[12] 144 if player.body.mesh != mesh { 145 player.replaceCollisionMesh(mesh) 146 } 147 player.scaleCollisionBody(20) 148 } 149 case 53110: 150 if *function.Arguments[0].Integer == 360 { 151 player.body.base = max(int32(20), int32(*function.Arguments[1].Integer)) 152 if remove { 153 player.body.base = int32(player.baseScale()) 154 } 155 player.scaleCollisionBody(1) 156 } 157 } 158 } 159 160 func collisionDirection(vector [3]float32) [3]float32 { 161 // QQR's small-vector normalization uses a second pass between its two thresholds. 162 length := math.Sqrt(surfaceDot(vector, vector)) 163 if length <= float64(float32(.00000001)) { 164 return [3]float32{} 165 } 166 for axis := range vector { 167 vector[axis] = float32(float64(vector[axis]) / length) 168 } 169 if length <= float64(float32(.0001)) { 170 length = math.Sqrt(surfaceDot(vector, vector)) 171 for axis := range vector { 172 vector[axis] = float32(float64(vector[axis]) / length) 173 } 174 } 175 return vector 176 } 177 178 func collisionDirectionOrUp(vector [3]float32) [3]float32 { 179 vector = collisionDirection(vector) 180 if vector == [3]float32{} { 181 return [3]float32{0, 1, 0} 182 } 183 return vector 184 } 185 186 func (player *character) land(height float32) { 187 movement := &player.movement 188 movement.velocity[1], movement.verticalVelocity = 0, 0 189 movement.speed = vectorLength(movement.velocity) 190 if movement.components[9] == 4 { 191 movement.action(player, 14) 192 } 193 movement.jumpHeight = 0 194 movement.fallHeight = height 195 movement.falling = false 196 } 197 198 func (player *character) jump() { 199 // Sources: client/game/kkr.dll/lifted-0033.c, sub_36071BD0; 200 // client/game/kkr.dll/lifted-0023.c, sub_36048FC0. 201 if player.movement.jumpHeight != 0 { 202 return 203 } 204 abilities := player.baseStats().Abilities 205 height := float32(1 + float64(float32(.005))*float64(uint32(abilities.Strength)+uint32(abilities.Agility))) 206 if player.geometry.physical(player.playfield) { 207 player.collisionBody() 208 end := player.position 209 end[1] += 100 210 contact := player.geometry.line(player.playfield, player.geometry.terrain[player.playfield], player.position, end, false) 211 if contact.hit { 212 clearance := contact.point[1] - player.position[1] 213 clearance = max(float32(.1), clearance-2*player.body.scale) 214 height = min(height, clearance) 215 } 216 } 217 218 // The native impulse stores mass*sqrt(2*abs(gravity)*height) before dividing by mass. 219 bodyMass, _ := player.movementMass() 220 impulse := float32(float64(bodyMass) * math.Sqrt(40*float64(height))) 221 player.movement.jumpHeight = height 222 player.movement.fallHeight = player.position[1] 223 player.movement.verticalVelocity = float32(float64(player.movement.verticalVelocity) + float64(1)/float64(bodyMass)*float64(impulse)) 224 player.movement.startFalling() 225 } 226 227 func (movement *playerMovement) startFalling() { 228 if !movement.falling { 229 movement.falling = true 230 movement.fallHeight = movement.previousPosition[1] 231 } 232 } 233 234 func collisionResponds(normalY float32) bool { 235 return normalY < .5 && normalY > -.5 || normalY < float32(-.9) || normalY > float32(.99) 236 } 237 238 func collisionUphill(normal [3]float32) [3]float32 { 239 // Keep the native two-stage expression, including its behavior for nonunit normals. 240 tangent := [3]float32{-normal[0], 1 - normal[1], -normal[2]} 241 dot := surfaceDot(tangent, normal) 242 for axis := range tangent { 243 tangent[axis] -= float32(float64(normal[axis]) * dot) 244 } 245 return tangent 246 } 247 248 func (player *character) guardSteepMovement(previous [3]float32) { 249 // Source: client/game/qqr.dll/lifted-0006.c, sub_42011D30. 250 direction := collisionDirection([3]float32{player.movement.velocity[0], 0, player.movement.velocity[2]}) 251 if direction == [3]float32{} { 252 forward, _ := bodyAxes(player.rotation) 253 direction = collisionDirection([3]float32{forward[0], 0, forward[2]}) 254 if direction == [3]float32{} { 255 direction = [3]float32{0, 0, 1} 256 } 257 } 258 start, end := player.position, player.position 259 start[1]++ 260 end[1]++ 261 for axis := range start { 262 start[axis] -= direction[axis] * .5 263 end[axis] += direction[axis] 264 } 265 contact := player.geometry.line(player.playfield, player.geometry.terrain[player.playfield], start, end, false) 266 if contact.hit && contact.normal[1] < .5 { 267 player.alignSurface(previous, false) 268 player.movement.velocity = [3]float32{} 269 player.movement.speed = 0 270 } 271 } 272 273 func (player *character) alignSurface(previous [3]float32, skipSegment bool) { 274 // Source: client/game/qqr.dll/lifted-0006.c, sub_42012A50. 275 movement := &player.movement 276 movement.normal = [3]float32{0, 1, 0} 277 if !player.geometry.physical(player.playfield) { 278 if movement.falling { 279 player.land(player.position[1]) 280 } 281 return 282 } 283 ground := player.geometry.terrain[player.playfield] 284 body := player.collisionBody() 285 radius := float32(math.Abs(float64(body.radius))) 286 if radius > .5 { 287 for axis := range body.offset { 288 body.offset[axis] /= 2 * radius 289 } 290 radius = .5 291 } 292 position, point := player.position, player.position 293 corrected := false 294 for remaining := 5; remaining > 0; remaining-- { 295 // Veto and all retries keep the same outer-frame historical position. 296 previousY := position[1] 297 retry := player.vetoPosition(&position, &previous) 298 if movement.falling && (position[1] != previousY || retry) { 299 point = [3]float32{} 300 probe := position 301 probe[1] += float32(1.2) * radius 302 player.geometry.floor(player.playfield, ground, probe, &point, &movement.normal) 303 movement.verticalVelocity = 0 304 position[1] = point[1] + float32(.01) 305 } 306 center, oldCenter := position, previous 307 for axis := range center { 308 center[axis] += body.offset[axis] 309 oldCenter[axis] += body.offset[axis] 310 } 311 if !skipSegment && surfaceDistanceSquared(previous, position) >= float64(radius*float32(.9))*float64(radius*float32(.9)) { 312 contact := player.geometry.line(player.playfield, ground, oldCenter, center, true) 313 if contact.hit { 314 point, movement.normal = contact.point, contact.normal 315 direction := collisionDirectionOrUp([3]float32{previous[0] - position[0], previous[1] - position[1], previous[2] - position[2]}) 316 if movement.normal[1] > .5 { 317 direction = [3]float32{0, 1, 0} 318 } 319 for axis := range center { 320 center[axis] = point[axis] + (radius+float32(.01))*direction[axis] 321 position[axis] = center[axis] - body.offset[axis] 322 } 323 retry, corrected, skipSegment = true, true, true 324 if movement.falling && movement.normal[1] > .5 { 325 player.land(position[1]) 326 } 327 } 328 } 329 330 // Raised probes reuse the first contact outputs even when the raised query misses. 331 contact := player.geometry.sphere(player.playfield, center, radius) 332 if contact.hit { 333 point, movement.normal = contact.point, contact.normal 334 } 335 if movement.normal == [3]float32{} { 336 movement.normal = collisionDirectionOrUp([3]float32{previous[0] - position[0], previous[1] - position[1], previous[2] - position[2]}) 337 } 338 if contact.hit { 339 raised := false 340 if !collisionResponds(movement.normal[1]) && movement.normal[1] > .5 && float64(movement.normal[1]) < .8 { 341 probe := center 342 probe[1] += .5 343 if contact = player.geometry.sphere(player.playfield, probe, radius); contact.hit { 344 point, movement.normal = contact.point, contact.normal 345 } 346 raised = true 347 } 348 if collisionResponds(movement.normal[1]) { 349 if raised { 350 point[1] -= .5 351 } 352 direction := [3]float32{center[0] - point[0], center[1] - point[1], center[2] - point[2]} 353 if raised { 354 direction[1] = 0 355 } 356 if direction != [3]float32{} && surfaceDot(direction, movement.normal) >= 0 { 357 direction = collisionDirectionOrUp(direction) 358 } else { 359 direction = movement.normal 360 } 361 for axis := range center { 362 center[axis] = point[axis] + (radius+float32(.01))*direction[axis] 363 position[axis] = center[axis] - body.offset[axis] 364 } 365 retry, corrected = true, true 366 } 367 } 368 369 // Project the body bottom, retaining raw liquid height separately from the floor. 370 bottom := position 371 for axis := range bottom { 372 bottom[axis] += body.offset[axis] 373 } 374 bottom[1] -= radius 375 probe := bottom 376 probe[1] += float32(1.2) * radius 377 water := player.geometry.floor(player.playfield, ground, probe, &point, &movement.normal) 378 clearance := float32(-9999) 379 if water.height > 0 { 380 clearance = water.height - point[1] 381 } 382 fallingEnabled := !player.flying() 383 deepWater := fallingEnabled && bottom[1] < water.height && clearance > float32(1.19) 384 if movement.deepWater != deepWater { 385 action := uint32(31) 386 if deepWater { 387 action = 24 388 } 389 movement.action(player, action) 390 } 391 if deepWater && movement.falling { 392 player.land(point[1]) 393 } 394 movement.deepWater = deepWater 395 if deepWater { 396 point[1] += clearance + float32(-.05) 397 if point[1]-bottom[1] <= 5 && point[1] > bottom[1] { 398 point[1] = float32(.1)*point[1] + float32(.9)*bottom[1] 399 } 400 } 401 if point[1] > bottom[1] { 402 bottom[1] = point[1] + float32(.01) 403 corrected = true 404 if movement.falling { 405 player.land(point[1] + float32(.01)) 406 } 407 } else if bottom[1]-radius*float32(.125) > point[1] { 408 if fallingEnabled { 409 movement.startFalling() 410 } 411 } else if bottom[1]-point[1] <= float32(.011) { 412 if movement.falling { 413 player.land(point[1] + float32(.01)) 414 } 415 } else if movement.velocity[1]+movement.verticalVelocity <= 0 && fallingEnabled { 416 bottom = point 417 bottom[1] += float32(.01) 418 corrected = true 419 if movement.falling { 420 player.land(point[1]) 421 } 422 } 423 for axis := range position { 424 position[axis] = bottom[axis] - body.offset[axis] 425 } 426 position[1] += radius 427 if !retry { 428 break 429 } 430 } 431 if corrected { 432 player.vetoPosition(&position, nil) 433 } 434 player.position = position 435 player.orientMovement() 436 }