File size: 1,991 Bytes
da17ede |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
extends AIController3D
class_name RobotAIController
@onready var robot: Robot = get_parent()
@onready var sensors: Array[Node] = $"../Sensors".get_children()
var steps_without_ball_hit_while_serving: int
var is_serving: bool
func reset():
super.reset()
func _physics_process(_delta):
n_steps += 1
if is_serving:
steps_without_ball_hit_while_serving += 1
if steps_without_ball_hit_while_serving > 400:
reward -= 1
robot.other_player.score += 1
robot.game_manager.reset_ball(robot.other_player)
func get_obs() -> Dictionary:
var ball_position = robot.to_local(robot.ball.global_position) / 8.0
var ball_goal_position = robot.goal.to_local(robot.ball.global_position) / 5.0
var robot_velocity = (
(robot.global_transform.basis.inverse() * robot.linear_velocity.limit_length(10.0)) / 10.0
)
var ball_velocity = (
(robot.global_transform.basis.inverse() * robot.ball.linear_velocity.limit_length(8.0))
/ 8.0
)
var observations: Array[float] = [
ball_position.y,
ball_position.z,
ball_goal_position.x,
ball_goal_position.z,
robot_velocity.y,
robot_velocity.z,
ball_velocity.y,
ball_velocity.z,
float(robot.jump_sensor.is_colliding()),
float(float(is_serving)),
float(robot.ball.ball_served),
robot.game_manager.get_hit_ball_count(robot),
steps_without_ball_hit_while_serving / 400.0
]
observations.append_array(get_raycast_sensor_obs())
return {"obs": observations}
func get_reward() -> float:
return reward
func get_action_space() -> Dictionary:
return {
"jump": {"size": 1, "action_type": "continuous"},
"movement": {"size": 1, "action_type": "continuous"}
}
func set_action(action) -> void:
robot.requested_movement = clamp(action.movement[0], -1.0, 1.0)
robot.jump_requested = action.jump[0] > 0
func get_raycast_sensor_obs():
var all_raycast_sensor_obs: Array[float] = []
for raycast_sensor in sensors:
all_raycast_sensor_obs.append_array(raycast_sensor.get_observation())
return all_raycast_sensor_obs
|