## Aim assist for the gamepad (A13), an option, on by default, and never applied to the ## mouse. Two gentle effects, both only for an opponent in plain sight close to where ## the player is already looking: ## ## - Friction: the right stick turns more slowly across an opponent, so a stick can ## settle on them instead of sliding past. ## - Pull: while aiming down sights and moving (the stick or the player), the look ## eases towards the opponent's chest, by no more than PULL_RADIANS_PER_SECOND. ## ## It only turns the client's look, the same as a player's own thumb, so it changes ## nothing the server judges. It never aims through cover: `clear` decides what is ## in sight, and the session passes the world's own line test. class_name AimAssist extends RefCounted ## How far either side of the look an opponent can be and still count. const CONE_RADIANS: float = deg_to_rad(6.0) ## Further than this, nobody counts. const RANGE_METRES: float = 90.0 ## Look speed at the very centre of an opponent, against 1 away from them. const FRICTION: float = 0.45 ## The fastest the pull turns the look. const PULL_RADIANS_PER_SECOND: float = deg_to_rad(9.0) ## The opponent nearest the look within the cone: {"point", "angle"}, or {} for none. ## `clear` takes (eye, point) and returns true when nothing is between them. static func nearest(eye: Vector3, yaw: float, pitch: float, targets: Array[Vector3], clear: Callable = Callable()) -> Dictionary: var look: Vector3 = CameraRig.look_direction(yaw, pitch) var best: Dictionary = {} var best_angle: float = CONE_RADIANS for point: Vector3 in targets: var to: Vector3 = point - eye var distance: float = to.length() if distance < 0.5 or distance > RANGE_METRES: continue var angle: float = look.angle_to(to / distance) if angle >= best_angle: continue if clear.is_valid() and not bool(clear.call(eye, point)): continue best_angle = angle best = {"point": point, "angle": angle} return best ## How much the stick's turn is slowed: 1 with nobody near the look, down to FRICTION ## at an opponent's centre. static func friction(found: Dictionary) -> float: if found.is_empty(): return 1.0 return lerpf(FRICTION, 1.0, clampf(float(found["angle"]) / CONE_RADIANS, 0.0, 1.0)) ## The look after `delta` seconds of pull towards `found`: Vector2(yaw, pitch). Pulls ## harder the closer the look already is, and never past the opponent. static func pull(eye: Vector3, yaw: float, pitch: float, found: Dictionary, delta: float) -> Vector2: if found.is_empty(): return Vector2(yaw, pitch) var to: Vector3 = Vector3(found["point"]) - eye var want_yaw: float = atan2(to.x, to.z) var want_pitch: float = atan2(to.y, Vector2(to.x, to.z).length()) var turn: Vector2 = Vector2(angle_difference(yaw, want_yaw), want_pitch - pitch) var closeness: float = 1.0 - clampf(float(found["angle"]) / CONE_RADIANS, 0.0, 1.0) var step: float = PULL_RADIANS_PER_SECOND * closeness * delta if turn.length() > step: turn = turn.normalized() * step return Vector2(fposmod(yaw + turn.x, TAU), clampf(pitch + turn.y, -InputSettings.PITCH_LIMIT, InputSettings.PITCH_LIMIT))