server/player_flight.go (4226B)
1 package main 2 3 import "math" 4 5 func (player *character) changeFlight(remove bool) { 6 movement := &player.movement 7 movement.prepare(player, movement.updatedAt) 8 action := uint32(27) 9 if remove { 10 action = 35 11 } 12 if !movement.admits(action) { 13 return 14 } 15 movement.components[5] = movement.components[3] 16 if remove { 17 player.leaveFlight() 18 movement.orientation = uint32(player.inventory.equipmentBonus(197) + player.nanoBonus(197)) 19 return 20 } 21 movement.components[0] = 7 22 23 // SetRelPos preserves V and q, aligns with the previous orientation mode, 24 // and stores the requested position as the fall-height history. 25 // Sources: kkr.dll 36046770/360468F0; qqr.dll 420108F0/42011D00. 26 position := player.position 27 position[1] += .5 28 player.placePosition(position) 29 movement.orientation = 4 30 } 31 32 func (player *character) leaveFlight() { 33 // The ground mode admits falling during position-only placement. Preserve 34 // the old orientation until the caller applies its callback's distinct rule. 35 // Source: client/game/kkr.dll/lifted-0022.c, sub_36046080/36046270/360468F0. 36 player.movement.components[0] = 2 37 if player.running { 38 player.movement.components[0] = 3 39 } 40 position := player.position 41 position[1] += .1 42 player.placePosition(position) 43 player.movement.vertical = 0 44 } 45 46 func (player *character) orientMovement() { 47 if player.movement.orientation != 0 && player.movement.orientation != 4 { 48 return 49 } 50 // QQR CalcOrientation modes0/4 use vector motion or the stopped body forward. 51 // Independent q, direct lateral controls and ground normal do not enter this frame. 52 forward, _ := bodyAxes(player.rotation) 53 if player.movement.speed != 0 { 54 forward = player.movement.velocity 55 if player.movement.orientation == 4 { 56 length := math.Sqrt(surfaceDot(forward, forward)) 57 for axis := range forward { 58 forward[axis] = float32(float64(forward[axis]) / length) 59 } 60 } 61 } 62 up := [3]float32{0, 1, 0} 63 if player.movement.orientation == 0 { 64 forward[1] = 0 65 forward = collisionDirection(forward) 66 if forward == [3]float32{} { 67 forward = [3]float32{0, 0, 1} 68 } 69 } else if forward != [3]float32{} { 70 for axis := range up { 71 up[axis] -= forward[axis] * forward[1] 72 } 73 up = collisionDirection(up) 74 if up == [3]float32{} { 75 up = [3]float32{0, 0, 1} 76 } 77 } 78 if player.movement.direction < 0 && player.movement.speed != 0 { 79 for axis := range forward { 80 forward[axis] = -forward[axis] 81 } 82 } 83 right := [3]float32{up[1]*forward[2] - up[2]*forward[1], up[2]*forward[0] - up[0]*forward[2], up[0]*forward[1] - up[1]*forward[0]} 84 rotation := [4]float32{up[2] - forward[1], forward[0] - right[2], right[1] - up[0], 1 + right[0] + up[1] + forward[2]} 85 length := float64(rotation[0])*float64(rotation[0]) + float64(rotation[1])*float64(rotation[1]) + float64(rotation[2])*float64(rotation[2]) + float64(rotation[3])*float64(rotation[3]) 86 if length > .01 { 87 for axis := range rotation { 88 rotation[axis] = float32(float64(rotation[axis]) / math.Sqrt(length)) 89 } 90 } else { 91 // FrameQuaternion's near-half-turn fallback uses MR's matrix constructor. 92 matrix := [3][3]float32{{right[0], up[0], forward[0]}, {right[1], up[1], forward[1]}, {right[2], up[2], forward[2]}} 93 trace := float64(matrix[0][0]) + float64(matrix[1][1]) + float64(matrix[2][2]) 94 if trace >= 0 { 95 scale := math.Sqrt(trace + 1) 96 rotation[3] = float32(.5 * scale) 97 rotation[0] = float32(float64(matrix[2][1]-matrix[1][2]) * (.5 / scale)) 98 rotation[1] = float32(float64(matrix[0][2]-matrix[2][0]) * (.5 / scale)) 99 rotation[2] = float32(float64(matrix[1][0]-matrix[0][1]) * (.5 / scale)) 100 } else { 101 index := 0 102 for axis := 1; axis < 3; axis++ { 103 if matrix[axis][axis] > matrix[index][index] { 104 index = axis 105 } 106 } 107 next, last := (index+1)%3, (index+2)%3 108 scale := math.Sqrt(1 + float64(matrix[index][index]) - float64(matrix[next][next]) - float64(matrix[last][last])) 109 rotation[index] = float32(.5 * scale) 110 rotation[next] = float32(float64(matrix[index][next]+matrix[next][index]) * (.5 / scale)) 111 rotation[last] = float32(float64(matrix[index][last]+matrix[last][index]) * (.5 / scale)) 112 rotation[3] = float32(float64(matrix[last][next]-matrix[next][last]) * (.5 / scale)) 113 } 114 } 115 player.rotation = rotation 116 }