# Robot 1 Improved
# Simple version for better robot movement

FIELD_LIMIT = 45 # Field boundaries
OBSTACLE_DIST = 15 # Safety distance for obstacles

while True:
# Sensor readings
ball_dir = robot.get_ball_direction() # -1 if no ball
compass = robot.get_compass_heading() # robot heading
sonar = robot.get_sonar_reading() # distance to obstacle
gps_x, gps_y = robot.get_gps_position() # robot position

# Check field boundaries
if abs(gps_x) > FIELD_LIMIT or abs(gps_y) > FIELD_LIMIT:
robot.set_speed(-5, 5) # move back inside
continue

# Avoid obstacles
if sonar < OBSTACLE_DIST:
robot.set_speed(-3, 5) # turn to avoid
continue

# Ball handling
if ball_dir == -1:
# Ball not detected -> rotate to search
robot.set_speed(4, -4)
else:
if abs(ball_dir) < 5:
# Ball ahead -> move forward
robot.set_speed(6, 6)
elif ball_dir > 0:
# Ball right -> turn right
robot.set_speed(5, 2)
else:
# Ball left -> turn left
robot.set_speed(2, 5)
