Assistant: 
User: User: extends CharacterBody3D

var SPEED = 3.0
const JUMP_VELOCITY = 13.0

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

var mouse_sens = 0.2

@onready var cam = get_node("cam")
@onready var audio_stream_player = get_node("shot_sound")
@onready var player_node = get_node("player")
@onready var animation_tree = get_node("player/payer/AnimationTree")
@onready var state_machine = animation_tree["parameters/playback"]
@onready var spring_arm2 = get_node("cam/aim")
@onready var shot_cooldown_timer = $shot_cooldown_timer
@onready var skeleton_ik = $player/payer/metarig/Skeleton3D/SkeletonIK3D

var time_in_air = 0.0

var TASK = ["FEET", "NONE", "NONE"]

func getTask(index):
	return TASK[index]

func setTask(index, task_name):
	TASK[index] = task_name

var camera_input : Vector2

var interpolate_spring = 0

var CURRENT_SPEED = 0.0

func _input(event):
	if get_tree().current_scene.get_node_or_null("ui/chatbox") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		if event.is_action_pressed("ui_cancel"):
			if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
				Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
			else:
				Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			camera_input = event.relative
		if event is InputEventKey and event.keycode == 82 and event.pressed:
			global_position = Vector3(0,2,0)
			setTask(0, "feets")

var target_point = Vector3.ZERO

var target_start = Vector3.ZERO

var title = "OpenWarfare v0.0"

func shoot():
	audio_stream_player.play()
	shot_cooldown_timer.start()

func _ready():
	skeleton_ik.start()

func _process(_delta):
	DisplayServer.window_set_title(title + " | fps: " + str(Engine.get_frames_per_second()))
	if Input.is_action_pressed("fire") and shot_cooldown_timer.is_stopped() and getTask(2) == "AIMING" and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		shoot()

var last_direction = Vector3(0, 0, 0)

var climb_progress = 0

var desired_rotation = Quaternion()

func handle_jumping():
	if Input.is_action_just_pressed("jump") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting() and (is_on_floor() or getTask(0) == "CLIMBING") and not get_node("check_over").is_colliding() and getTask(0) != "ROLLING":
			if getTask(0) == "CLIMBING":
				climb_cooldown = 0.25
			setTask(0, "JUMPING")
			animation_tree["parameters/feets/TimeSeek/seek_request"] = 0.0
			attempt_climb()
			velocity.y = JUMP_VELOCITY

func handle_rolling():
	if Input.is_action_just_pressed("roll") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting() and getTask(0) != "ROLLING":
		if getTask(0) == "CLIMBING":
			velocity.y = 11
		setTask(0, "ROLLING")
		state_machine.travel("roll")

func handle_climbing(delta):
	if getTask(0) != "CLIMBING" or climb_cooldown > 0.0:
		get_node("player/payer/AnimationTree")["parameters/feets/climb/blend_amount"] = lerp(float(get_node("player/payer/AnimationTree")["parameters/feets/climb/blend_amount"]), 0.0, delta*10)
		climb_cooldown = max(climb_cooldown-delta, 0.0)
		return false

	var input_strength = Input.get_action_strength("right") - Input.get_action_strength("left")
	if get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		input_strength = 0.0
	var direction = player_node.get_global_transform().basis * Vector3(animation_tree["parameters/feets/BlendSpace1D/blend_position"] * 4, 0, 0)
	var blend_position = animation_tree["parameters/feets/BlendSpace1D/blend_position"] * 4
	var is_colliding_left = get_node("player/SpringArm3D/MeshInstance3D2/left").is_colliding()
	var is_colliding_right = get_node("player/SpringArm3D/MeshInstance3D2/right").is_colliding()
	
	if not is_colliding_left and blend_position > 0.0:
		direction = player_node.get_global_transform().basis * Vector3(0, 0, 0)
	elif not is_colliding_right and blend_position < 0.0:
		direction = player_node.get_global_transform().basis * Vector3(0, 0, 0)

	animation_tree["parameters/feets/climb/blend_amount"] = lerp(animation_tree["parameters/feets/climb/blend_amount"], 1.0, delta * 15)
	animation_tree["parameters/feets/BlendSpace1D/blend_position"] = lerp(animation_tree["parameters/feets/BlendSpace1D/blend_position"], input_strength, delta * 7)

	climb_progress = min(climb_progress + delta * 10, 1.0)

	var middle_node = get_node("player/SpringArm3D/MeshInstance3D2/middle")

	desired_rotation = Quaternion(Vector3.BACK, middle_node.get_collision_normal()).normalized()
	player_node.global_transform.basis = Basis(player_node.global_transform.basis.get_rotation_quaternion().slerp(desired_rotation, climb_progress))

	var r_point = middle_node.get_collision_point()
	target_point = Vector3(r_point.x, target_point.y, r_point.z) + (player_node.get_global_transform().basis*Vector3(0,0,0.25))
	global_position = global_position.lerp(target_point, climb_progress)

	velocity.x = direction.x
	velocity.z = direction.z

	move_and_slide()
	
	handle_jumping()
	
	handle_rolling()
	
	if Input.is_action_pressed("crouch") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		setTask(0, "FALLING")
		climb_cooldown = 0.25
	climb_cooldown -= delta

	return true

func handle_aiming(delta):
	interpolate_spring = clamp(interpolate_spring+(delta*1), 0, 1)
	var ui_aim = get_tree().current_scene.get_node("ui/aim")
	var lerp_value: float
	var input_dir = Vector2(Input.get_action_strength("right") - Input.get_action_strength("left"), Input.get_action_strength("forward") - Input.get_action_strength("backward")).normalized()
	if get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		input_dir = Vector2(0.0, 0.0)
	var input_magnitude = -clamp(abs(input_dir.y) + abs(input_dir.x), 0, 1)
	
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and getTask(2) != "AIMING" and (is_on_floor() and not Input.is_action_pressed("sprint") and getTask(0) != "ROLLING") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		setTask(2, "AIMING")
		interpolate_spring = 0
	if getTask(2) == "AIMING" and (not Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) or not is_on_floor() or getTask(0) == "ROLLING") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		setTask(2, "NONE")
		interpolate_spring = 0
	
	var direction = Vector3.ZERO
	if getTask(2) == "AIMING":
		get_node("Node3D").rotation_degrees = Vector3(-cam.rotation_degrees.x + 25, (cam.rotation_degrees.y - 180) - 10, 0)
		cam.spring_length = lerp(cam.spring_length, 0.75, interpolate_spring)
		spring_arm2.spring_length = lerp(spring_arm2.spring_length, 0.5, interpolate_spring)
		mouse_sens = 0.15
		ui_aim.modulate.a = clamp(ui_aim.modulate.a + (delta * 10), 0, 1)
		direction = player_node.get_global_transform().basis * Vector3(input_dir.x, 0, -input_dir.y)
		lerp_value = 2 if input_magnitude != 0 else 7
	else:
		cam.spring_length = lerp(cam.spring_length, 2.0, interpolate_spring)
		spring_arm2.spring_length = lerp(spring_arm2.spring_length, 0.0, interpolate_spring)
		mouse_sens = 0.2
		ui_aim.modulate.a = clamp(ui_aim.modulate.a - (delta * 10), 0, 1)
		direction = player_node.get_global_transform().basis * Vector3(0, 0, -1)
		lerp_value = 2
	
	animation_tree["parameters/feets/aim_blend/blend_amount"] = ui_aim.modulate.a
	skeleton_ik.interpolation = ui_aim.modulate.a
	animation_tree["parameters/feets/shot/blend_amount"] = lerp(animation_tree["parameters/feets/shot/blend_amount"], 0.0 if shot_cooldown_timer.is_stopped() else 1.0, delta*10)
	
	cam.rotation_degrees.x = clamp(cam.rotation_degrees.x - camera_input.y * mouse_sens, -80, 60)
	cam.rotation_degrees.y = wrapf(cam.rotation_degrees.y - camera_input.x * mouse_sens, 0, 360)
	camera_input = Vector2.ZERO
	
	var cam_rotation = cam.get_rotation().y
	var target_rotation = cam_rotation + deg_to_rad(rad_to_deg(atan2(input_dir.y, input_dir.x)) - 90)
	var difference = wrapf(target_rotation - player_node.get_rotation().y, -PI, PI)
	var new_rotation = player_node.get_rotation().y + sign(difference) * min(abs(difference), 10 * delta)
	
	if getTask(2) == "AIMING" or input_magnitude != 0:
		player_node.set_rotation(Vector3(0, cam.get_rotation().y if getTask(2) == "AIMING" else new_rotation, 0))
	
	animation_tree["parameters/feets/aim/blend_position"].x = lerp(animation_tree["parameters/feets/aim/blend_position"].x, input_dir.x if getTask(2) == "AIMING" else 0.0, delta * lerp_value)
	animation_tree["parameters/feets/aim/blend_position"].y = lerp(animation_tree["parameters/feets/aim/blend_position"].y, input_dir.y if getTask(2) == "AIMING" else 0.0, delta * lerp_value)
	animation_tree["parameters/feets/aim_crouched/blend_position"] = animation_tree["parameters/feets/aim/blend_position"]
	return direction

func attempt_climb():
	if climb_cooldown > 0.0:
		return

	var spring_arm = get_node("player/SpringArm3D")
	var right_ray = get_node("player/SpringArm3D/MeshInstance3D2/right")
	var left_ray = get_node("player/SpringArm3D/MeshInstance3D2/left")
	var climb_ray = get_node("player/SpringArm3D/MeshInstance3D/raycast_climb")
	var climb_after_ray = get_node("player/SpringArm3D/MeshInstance3D/raycast_climb_after")

	var conditions_met = spring_arm.get_hit_length() < 0.75 and \
						right_ray.is_colliding() and \
						left_ray.is_colliding() and \
						not Input.is_action_pressed("crouch") and \
						velocity.y >= -25 and \
						velocity.y < -5 and \
						getTask(0) == "JUMPING" and \
						climb_ray.is_colliding() and \
						not climb_after_ray.is_colliding() and \
						spring_arm.get_hit_length() != spring_arm.spring_length
	
	if not conditions_met:
		return

	setTask(0, "CLIMBING")
	velocity = Vector3.ZERO
	reset_animation_tree()
	
	desired_rotation = Quaternion(Vector3.BACK, climb_ray.get_collision_normal()).normalized()
	update_target_point(climb_ray.get_collision_point())
	climb_progress = 0
	CURRENT_SPEED = 0.0
	LAST_CURRENT_SPEED = 0.0

func reset_animation_tree():
	animation_tree["parameters/feets/BlendSpace1D/blend_position"] = 0.0
	animation_tree["parameters/feets/TimeSeek 2/seek_request"] = 0.0
	setTask(1, "none")
	interpolate_spring = 0

func update_target_point(collision_point):
	target_point = collision_point + (player_node.global_transform.basis * Vector3(0, 0, 0.25))
	target_start = global_position

var LAST_CURRENT_SPEED = 0.0

var climb_cooldown = 0.25

func _physics_process(delta):
	
	var direction = Vector3.ZERO
	
	var crouch_blend_amount = animation_tree["parameters/feets/jump_c/blend_amount"]
	crouch_blend_amount = delta * 7 if Input.is_action_pressed("crouch") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting() else -delta * 7
	crouch_blend_amount = clamp(animation_tree["parameters/feets/jump_c/blend_amount"] + crouch_blend_amount, 0, 1)
	animation_tree["parameters/feets/jump_c/blend_amount"] = crouch_blend_amount
	animation_tree["parameters/feets/Blend2 3/blend_amount"] = crouch_blend_amount
	
	attempt_climb()
	
	direction = handle_aiming(delta)
	
	if handle_climbing(delta):
		return
	
	if not is_on_floor():
		var mass = 0.75
		velocity.y -= ((gravity*mass)*delta)
	
	if not is_on_floor() and getTask(0) == "FEET":
		setTask(0, "FALLING")
	
	if getTask(0) == "FALLING":
		animation_tree["parameters/feets/falling/blend_amount"] = lerp(float(animation_tree["parameters/feets/falling/blend_amount"]), 1.0, delta*2)
	else:
		animation_tree["parameters/feets/falling/blend_amount"] = lerp(float(animation_tree["parameters/feets/falling/blend_amount"]), 0.0, delta*5)
	
	if is_on_floor() and (getTask(0) == "JUMPING" or getTask(0) == "FALLING"):
		if time_in_air > 0.2:
			animation_tree.set("parameters/feets/OneShot/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
		setTask(0, "FEET")
		state_machine.travel("feets")
	
	if getTask(0) == "JUMPING":
		animation_tree["parameters/feets/jump/blend_amount"] = lerp(float(animation_tree["parameters/feets/jump/blend_amount"]), 1.0, delta*15)
	else:
		animation_tree["parameters/feets/jump/blend_amount"] = lerp(float(animation_tree["parameters/feets/jump/blend_amount"]), 0.0, delta*10)
	
	handle_rolling()
	
	handle_jumping()
	
	var target_speed: float
	var lerp_factor: float
	var input_dir = Vector2(Input.get_action_strength("right") - Input.get_action_strength("left"), Input.get_action_strength("forward") - Input.get_action_strength("backward")).normalized()
	if get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		input_dir = Vector2(0.0, 0.0)
	var input_magnitude = -clamp(abs(input_dir.y) + abs(input_dir.x), 0, 1)
	
	if input_magnitude != 0:
		if getTask(2) != "AIMING":
			if Input.is_action_pressed("sprint") and getTask(1) != "CROUCHED":
				target_speed = SPEED * 2.5
				lerp_factor = 4
			elif Input.is_action_pressed("walk") and getTask(1) != "CROUCHED":
				target_speed = SPEED * 0.4
				lerp_factor = 4
			else:
				target_speed = SPEED if getTask(1) != "CROUCHED" else SPEED * 0.4
				lerp_factor = 15
		else:
			target_speed = SPEED * 0.3
			lerp_factor = 15
	else:
		target_speed = 0.0
		lerp_factor = 10
	
	CURRENT_SPEED = lerp(CURRENT_SPEED, target_speed, delta * lerp_factor)
	
	var cam_position_y_target = 0.7
	var blend_amount_delta = delta * 7
	var height_target = 2
	var position_y_target = 0

	if getTask(1) == "CROUCHED" or getTask(0) == "ROLLING":
		cam_position_y_target -= 0.5
		blend_amount_delta *= 1
		height_target = 1.5
		position_y_target = -0.25
	else:
		blend_amount_delta *= -1

	cam.position.y = clamp(cam.position.y + delta * 2 * sign(cam_position_y_target - cam.position.y), 0.7 - 0.5, 0.7)
	animation_tree["parameters/feets/aim_crouch/blend_amount"] = clamp(animation_tree["parameters/feets/aim_crouch/blend_amount"] + blend_amount_delta, 0, 1)

	var standing = get_node("standing").get_shape()
	if standing.height != height_target:
		standing.height = height_target
		get_node("standing").position.y = position_y_target
	
	if (Input.is_action_pressed("crouch") or getTask(0) == "ROLLING") and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		setTask(1, "CROUCHED")
	elif not get_node("check_over").is_colliding() and getTask(1) == "CROUCHED" and not get_tree().current_scene.get_node("ui/chatbox").is_chatting():
		setTask(1, "NONE")
	
	var blend_position = animation_tree["parameters/feets/feets/blend_position"]
	
	if Input.is_action_pressed("walk") and getTask(1) != "CROUCHED" and not Input.is_action_pressed("sprint"):
		blend_position.y = min(blend_position.y + delta * 7, 1)
	elif getTask(1) == "CROUCHED":
		blend_position.y = max(blend_position.y - delta * 7, -1)
	elif getTask(1) != "CROUCHED" and blend_position.y != 0 and not Input.is_action_pressed("walk"):
		blend_position.y = lerp(blend_position.y, 0.0, delta * 3)
	
	blend_position.x = lerp(blend_position.x, (CURRENT_SPEED / SPEED) - 1, delta * (15 if input_magnitude == 0 else 4))
	animation_tree["parameters/feets/feets/blend_position"] = blend_position
	
	var floor_lerp_factor = 1
	if is_on_floor():
		floor_lerp_factor = delta * 10 if input_magnitude == 0 else 1
	else:
		floor_lerp_factor = delta * 5
	
	var speed_factor = min(CURRENT_SPEED, SPEED * 0.5) if getTask(2) == "AIMING" else SPEED * snapped(blend_position.x + 1, 0.1)
	velocity.x = lerp(velocity.x, direction.x * speed_factor, floor_lerp_factor)
	velocity.z = lerp(velocity.z, direction.z * speed_factor, floor_lerp_factor)
	
	if getTask(0) == "ROLLING":
		direction = player_node.get_global_transform().basis * Vector3(0, 0, -CURRENT_SPEED)
		velocity.x = direction.x
		velocity.z = direction.z
		CURRENT_SPEED = max(LAST_CURRENT_SPEED, 6.0)
	
	LAST_CURRENT_SPEED = CURRENT_SPEED
	
	time_in_air = 0.0 if is_on_floor() else time_in_air + delta
	
	move_and_slide()

func _on_animation_tree_animation_finished(anim_name):
	if anim_name == "roll":
		setTask(0, "FEET")
	pass # Replace with function body.



optimize this code and make it better, remember it's godot 4.2.2 version
Assistant: Optimizing code can involve several strategies, including reducing redundancy, improving readability, and ensuring efficient use of resources. Below is a refactored version of your Godot script, focusing on these aspects while maintaining the original functionality. 

```gdscript
extends CharacterBody3D

var SPEED = 3.0
const JUMP_VELOCITY = 13.0
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var mouse_sens = 0.2

@onready var cam = get_node("cam")
@onready var audio_stream_player = get_node("shot_sound")
@onready var player_node = get_node("player")
@onready var animation_tree = get_node("player/payer/AnimationTree")
@onready var state_machine = animation_tree["parameters/playback"]
@onready var spring_arm2 = get_node("cam/aim")
@onready var shot_cooldown_timer = $shot_cooldown_timer
@onready var skeleton_ik = $player/payer/metarig/Skeleton3D/SkeletonIK3D

var time_in_air = 0.0
var TASK = ["FEET", "NONE", "NONE"]
var camera_input : Vector2
var interpolate_spring = 0
var CURRENT_SPEED = 0.0

func getTask(index):
    return TASK[index]

func setTask(index, task_name):
    TASK[index] = task_name

func _input(event):
    var chatbox = get_tree().current_scene.get_node_or_null("ui/chatbox")
    if chatbox and not chatbox.is_chatting():
        if event.is_action_pressed("ui_cancel"):
            Input.set_mouse_mode(Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE ? Input.MOUSE_MODE_CAPTURED : Input.MOUSE_MODE_VISIBLE)
        if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
            camera_input = event.relative
        if event is InputEventKey and event.keycode == 82 and event.pressed:
            global_position = Vector3(0, 2, 0)
            setTask(0, "feets")

func shoot():
    audio_stream_player.play()
    shot_cooldown_timer.start()

func _ready():
    skeleton_ik.start()

func _process(_delta):
    DisplayServer.window_set_title("OpenWarfare v0.0 | fps
User: show ful code
Assistant:
Assistant: