use carla::client::{ActorBase, Client};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Connecting to CARLA simulator...");
let client = Client::connect("localhost", 2000, None)?;
println!("Connected!");
println!("\nGetting current world...");
let mut world = client.world()?;
println!("World ready!");
let blueprint_library = world.blueprint_library()?;
let vehicle_bp = blueprint_library
.find("vehicle.tesla.model3")?
.expect("Tesla Model 3 not found");
let spawn_points = world.map()?.recommended_spawn_points()?;
println!("Available spawn points: {}", spawn_points.len());
let spawn_count = 5.min(spawn_points.len());
println!("\nSpawning {} vehicles...", spawn_count);
let mut vehicles = Vec::new();
for i in 0..spawn_count {
let spawn_point = spawn_points.get(i).unwrap();
match world.spawn_actor(&vehicle_bp, spawn_point) {
Ok(vehicle) => {
println!(" ✓ Vehicle {} spawned (ID: {})", i + 1, vehicle.id());
vehicles.push(vehicle);
}
Err(e) => {
eprintln!(" ✗ Failed to spawn vehicle {}: {}", i + 1, e);
}
}
}
println!("\nSuccessfully spawned {} vehicles", vehicles.len());
println!("\nVehicle summary:");
for (i, vehicle) in vehicles.iter().enumerate() {
let location = vehicle.location()?;
let alive = vehicle.is_alive()?;
println!(
" Vehicle {}: ID={}, alive={}, location=({:.1}, {:.1}, {:.1})",
i + 1,
vehicle.id(),
alive,
location.x,
location.y,
location.z
);
}
println!("\nVehicles will remain in the simulation.");
println!("Restart CARLA to clean up spawned actors.");
Ok(())
}