carla 0.14.1

Rust client library for Carla simulator
Documentation
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Batch Operations Tests
//!
//! Tests for batch command execution (Phase 10.4).
//!
//! # Test Categories
//! - Batch spawn/destroy operations
//! - Batch control operations (vehicles, walkers)
//! - Response handling and error management
//! - Performance verification
//!
//! Run with:
//! ```bash
//! cargo run --example test_batch_commands --profile dev-release
//! ```

use carla::{
    client::Client,
    geom::Location,
    rpc::{Command, VehicleControl, WalkerControl},
};
use std::{thread, time::Duration};

type TestResult = Result<(), Box<dyn std::error::Error>>;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== Batch Operations Tests ===\n");

    let mut client = Client::connect("127.0.0.1", 2000, None)?;
    let mut world = client.world()?;
    println!("Connected to CARLA server\n");

    let mut passed = 0;
    let mut failed = 0;

    // Spawn/Destroy tests
    println!("--- Spawn/Destroy Operations ---");
    run_test(
        "test_batch_spawn_actors",
        || test_batch_spawn_actors(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_batch_destroy_actors",
        || test_batch_destroy_actors(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );

    // Control tests
    println!("\n--- Control Operations ---");
    run_test(
        "test_batch_apply_vehicle_control",
        || test_batch_apply_vehicle_control(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_batch_apply_walker_control",
        || test_batch_apply_walker_control(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );

    // Response handling tests
    println!("\n--- Response Handling ---");
    run_test(
        "test_batch_command_response",
        || test_batch_command_response(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_batch_error_handling",
        || test_batch_error_handling(&mut client),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_batch_partial_failure",
        || test_batch_partial_failure(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );

    // Batch behavior tests
    println!("\n--- Batch Behavior ---");
    run_test(
        "test_batch_order_preservation",
        || test_batch_order_preservation(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_empty_batch",
        || test_empty_batch(&mut client),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_large_batch",
        || test_large_batch(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );

    // Performance and mixed tests
    println!("\n--- Performance & Mixed Operations ---");
    run_test(
        "test_batch_performance",
        || test_batch_performance(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );
    run_test(
        "test_mixed_command_types",
        || test_mixed_command_types(&mut client, &mut world),
        &mut passed,
        &mut failed,
    );

    println!("\n=== Results ===");
    println!("Passed: {}", passed);
    println!("Failed: {}", failed);
    std::process::exit(if failed > 0 { 1 } else { 0 });
}

fn run_test<F>(name: &str, test_fn: F, passed: &mut i32, failed: &mut i32)
where
    F: FnOnce() -> TestResult,
{
    print!("Testing {}... ", name);
    match test_fn() {
        Ok(_) => {
            println!("✓ PASS");
            *passed += 1;
        }
        Err(e) => {
            println!("✗ FAIL: {}", e);
            *failed += 1;
        }
    }
}

// ===== Spawn/Destroy Tests =====

fn test_batch_spawn_actors(client: &mut Client, world: &mut carla::client::World) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_count = 5.min(spawn_points.len());

    // Create batch spawn commands
    let mut commands = Vec::new();
    for i in 0..spawn_count {
        let spawn_point = spawn_points.get(i).unwrap();
        commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_point.clone(),
            None,
        ));
    }

    // Execute batch spawn
    let responses = client.apply_batch_sync(commands, false)?;

    // Verify all spawned successfully
    assert_eq!(
        responses.len(),
        spawn_count,
        "Should return one response per command"
    );

    let success_count = responses.iter().filter(|r| r.is_success()).count();
    assert!(success_count > 0, "At least some vehicles should spawn");

    // Cleanup: destroy spawned actors
    let mut destroy_commands = Vec::new();
    for response in responses {
        if let Some(actor_id) = response.actor_id() {
            destroy_commands.push(Command::destroy_actor(actor_id));
        }
    }
    client.apply_batch_sync(destroy_commands, false)?;

    Ok(())
}

fn test_batch_destroy_actors(client: &mut Client, world: &mut carla::client::World) -> TestResult {
    // First spawn some actors
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_count = 3.min(spawn_points.len());

    let mut spawn_commands = Vec::new();
    for i in 0..spawn_count {
        spawn_commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_points.get(i).unwrap().clone(),
            None,
        ));
    }

    let spawn_responses = client.apply_batch_sync(spawn_commands, false)?;

    // Collect actor IDs
    let actor_ids: Vec<_> = spawn_responses
        .iter()
        .filter_map(|r| r.actor_id())
        .collect();

    assert!(!actor_ids.is_empty(), "Should have spawned some actors");

    // Create batch destroy commands
    let mut destroy_commands = Vec::new();
    for &actor_id in &actor_ids {
        destroy_commands.push(Command::destroy_actor(actor_id));
    }

    // Execute batch destroy
    let destroy_responses = client.apply_batch_sync(destroy_commands, false)?;

    // Verify responses
    assert_eq!(
        destroy_responses.len(),
        actor_ids.len(),
        "Should return one response per destroy command"
    );

    Ok(())
}

// ===== Control Tests =====

fn test_batch_apply_vehicle_control(
    client: &mut Client,
    world: &mut carla::client::World,
) -> TestResult {
    // Spawn vehicles
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_count = 3.min(spawn_points.len());

    let mut spawn_commands = Vec::new();
    for i in 0..spawn_count {
        spawn_commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_points.get(i).unwrap().clone(),
            None,
        ));
    }

    let spawn_responses = client.apply_batch_sync(spawn_commands, false)?;
    let vehicle_ids: Vec<_> = spawn_responses
        .iter()
        .filter_map(|r| r.actor_id())
        .collect();

    assert!(!vehicle_ids.is_empty(), "Should have spawned vehicles");

    // Create batch control commands
    let mut control_commands = Vec::new();
    for &actor_id in &vehicle_ids {
        let control = VehicleControl {
            throttle: 0.5,
            steer: 0.0,
            brake: 0.0,
            hand_brake: false,
            reverse: false,
            manual_gear_shift: false,
            gear: 0,
        };
        control_commands.push(Command::apply_vehicle_control(actor_id, control));
    }

    // Execute batch control
    let control_responses = client.apply_batch_sync(control_commands, false)?;

    assert_eq!(
        control_responses.len(),
        vehicle_ids.len(),
        "Should return one response per control command"
    );

    // Cleanup
    let destroy_commands: Vec<_> = vehicle_ids
        .iter()
        .map(|&id| Command::destroy_actor(id))
        .collect();
    client.apply_batch_sync(destroy_commands, false)?;

    Ok(())
}

fn test_batch_apply_walker_control(
    client: &mut Client,
    world: &mut carla::client::World,
) -> TestResult {
    // Spawn walkers
    let blueprint_library = world.blueprint_library()?;
    let walker_bp = blueprint_library
        .find("walker.pedestrian.0001")?
        .ok_or("Walker blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_count = 3.min(spawn_points.len());

    let mut spawn_commands = Vec::new();
    for i in 0..spawn_count {
        spawn_commands.push(Command::spawn_actor(
            walker_bp.clone(),
            spawn_points.get(i).unwrap().clone(),
            None,
        ));
    }

    let spawn_responses = client.apply_batch_sync(spawn_commands, false)?;
    let walker_ids: Vec<_> = spawn_responses
        .iter()
        .filter_map(|r| r.actor_id())
        .collect();

    if walker_ids.is_empty() {
        // Skip test if no walkers spawned (some maps may not support walkers)
        return Ok(());
    }

    // Create batch walker control commands
    let mut control_commands = Vec::new();
    for &actor_id in &walker_ids {
        use carla_sys::carla::geom::Vector3D as FfiVector3D;
        let direction = FfiVector3D {
            x: 1.0,
            y: 0.0,
            z: 0.0,
        };
        let control = WalkerControl {
            direction,
            speed: 1.4,
            jump: false,
        };
        control_commands.push(Command::apply_walker_control(actor_id, control));
    }

    // Execute batch control
    let control_responses = client.apply_batch_sync(control_commands, false)?;

    assert_eq!(
        control_responses.len(),
        walker_ids.len(),
        "Should return one response per control command"
    );

    // Cleanup
    let destroy_commands: Vec<_> = walker_ids
        .iter()
        .map(|&id| Command::destroy_actor(id))
        .collect();
    client.apply_batch_sync(destroy_commands, false)?;

    Ok(())
}

// ===== Response Handling Tests =====

fn test_batch_command_response(
    client: &mut Client,
    world: &mut carla::client::World,
) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_point = spawn_points.get(0).ok_or("No spawn points available")?;

    // Create a single spawn command
    let commands = vec![Command::spawn_actor(
        vehicle_bp.clone(),
        spawn_point.clone(),
        None,
    )];

    let responses = client.apply_batch_sync(commands, false)?;

    // Test response structure
    assert_eq!(responses.len(), 1, "Should have one response");

    let response = &responses[0];
    assert!(response.is_success(), "Command should succeed");
    assert!(!response.has_error(), "Should not have error");
    assert!(response.error().is_none(), "Error should be None");
    assert!(response.actor_id().is_some(), "Should have actor ID");

    // Cleanup
    if let Some(actor_id) = response.actor_id() {
        client.apply_batch_sync(vec![Command::destroy_actor(actor_id)], false)?;
    }

    Ok(())
}

fn test_batch_error_handling(client: &mut Client) -> TestResult {
    // Try to destroy a non-existent actor (should fail gracefully)
    let invalid_id = 999999;
    let commands = vec![Command::destroy_actor(invalid_id)];

    let responses = client.apply_batch_sync(commands, false)?;

    assert_eq!(responses.len(), 1, "Should have one response");

    // The response might succeed (CARLA doesn't always error on invalid destroy)
    // Just verify we get a response
    let _response = &responses[0];

    Ok(())
}

fn test_batch_partial_failure(client: &mut Client, world: &mut carla::client::World) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;

    // Mix valid and potentially invalid commands
    let mut commands = Vec::new();

    // Valid spawn
    if let Some(spawn_point) = spawn_points.get(0) {
        commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_point.clone(),
            None,
        ));
    }

    // Invalid destroy (non-existent actor)
    commands.push(Command::destroy_actor(999999));

    // Another valid spawn
    if let Some(spawn_point) = spawn_points.get(1) {
        commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_point.clone(),
            None,
        ));
    }

    let command_count = commands.len();
    let responses = client.apply_batch_sync(commands, false)?;

    assert_eq!(
        responses.len(),
        command_count,
        "Should have one response per command"
    );

    // Cleanup any spawned actors
    let mut destroy_commands = Vec::new();
    for response in responses {
        if let Some(actor_id) = response.actor_id() {
            destroy_commands.push(Command::destroy_actor(actor_id));
        }
    }
    if !destroy_commands.is_empty() {
        client.apply_batch_sync(destroy_commands, false)?;
    }

    Ok(())
}

// ===== Batch Behavior Tests =====

fn test_batch_order_preservation(
    client: &mut Client,
    world: &mut carla::client::World,
) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_count = 3.min(spawn_points.len());

    // Create commands with specific order
    let mut commands = Vec::new();
    for i in 0..spawn_count {
        commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_points.get(i).unwrap().clone(),
            None,
        ));
    }

    let responses = client.apply_batch_sync(commands, false)?;

    // Verify response count matches command count (order is preserved)
    assert_eq!(
        responses.len(),
        spawn_count,
        "Responses should match command count and order"
    );

    // Cleanup
    let destroy_commands: Vec<_> = responses
        .iter()
        .filter_map(|r| r.actor_id())
        .map(Command::destroy_actor)
        .collect();
    client.apply_batch_sync(destroy_commands, false)?;

    Ok(())
}

fn test_empty_batch(client: &mut Client) -> TestResult {
    // Execute empty batch
    let commands: Vec<Command> = Vec::new();
    let responses = client.apply_batch_sync(commands, false)?;

    assert_eq!(responses.len(), 0, "Empty batch should return no responses");

    Ok(())
}

fn test_large_batch(client: &mut Client, world: &mut carla::client::World) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;

    // Create a large batch (up to 50 or available spawn points)
    let batch_size = 50.min(spawn_points.len());

    let mut commands = Vec::new();
    for i in 0..batch_size {
        commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_points.get(i % spawn_points.len()).unwrap().clone(),
            None,
        ));
    }

    let responses = client.apply_batch_sync(commands, false)?;

    assert_eq!(
        responses.len(),
        batch_size,
        "Should handle large batches correctly"
    );

    // Count successes
    let success_count = responses.iter().filter(|r| r.is_success()).count();
    assert!(success_count > 0, "At least some spawns should succeed");

    // Cleanup
    let destroy_commands: Vec<_> = responses
        .iter()
        .filter_map(|r| r.actor_id())
        .map(Command::destroy_actor)
        .collect();

    // Destroy in batches to avoid overwhelming the server
    if !destroy_commands.is_empty() {
        client.apply_batch_sync(destroy_commands, false)?;
    }

    Ok(())
}

// ===== Performance & Mixed Tests =====

fn test_batch_performance(client: &mut Client, world: &mut carla::client::World) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let test_size = 10.min(spawn_points.len());

    // Create batch commands
    let mut commands = Vec::new();
    for i in 0..test_size {
        commands.push(Command::spawn_actor(
            vehicle_bp.clone(),
            spawn_points.get(i).unwrap().clone(),
            None,
        ));
    }

    // Measure batch execution time
    let start = std::time::Instant::now();
    let responses = client.apply_batch_sync(commands, false)?;
    let batch_duration = start.elapsed();

    // Verify it executed reasonably fast (batch should be quick)
    assert!(
        batch_duration.as_secs() < 5,
        "Batch should complete in reasonable time"
    );

    let success_count = responses.iter().filter(|r| r.is_success()).count();
    assert!(success_count > 0, "Some spawns should succeed");

    // Cleanup
    let destroy_commands: Vec<_> = responses
        .iter()
        .filter_map(|r| r.actor_id())
        .map(Command::destroy_actor)
        .collect();
    client.apply_batch_sync(destroy_commands, false)?;

    Ok(())
}

fn test_mixed_command_types(client: &mut Client, world: &mut carla::client::World) -> TestResult {
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .find("vehicle.tesla.model3")?
        .ok_or("Vehicle blueprint not found")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_point = spawn_points.get(0).ok_or("No spawn points available")?;

    // Mix different command types in one batch
    let commands = vec![Command::spawn_actor(
        vehicle_bp.clone(),
        spawn_point.clone(),
        None,
    )];

    let responses = client.apply_batch_sync(commands, false)?;

    let actor_id = responses[0].actor_id().ok_or("Failed to spawn vehicle")?;

    // Now create a mixed batch with different operations on the same actor
    let mut mixed_commands = Vec::new();

    // 2. Apply control
    let control = VehicleControl {
        throttle: 0.3,
        steer: 0.0,
        brake: 0.0,
        hand_brake: false,
        reverse: false,
        manual_gear_shift: false,
        gear: 0,
    };
    mixed_commands.push(Command::apply_vehicle_control(actor_id, control));

    // 3. Apply location change
    let new_location = Location::new(
        spawn_point.location.x + 5.0,
        spawn_point.location.y,
        spawn_point.location.z,
    );
    mixed_commands.push(Command::ApplyLocation {
        actor_id,
        location: new_location,
    });

    // 4. Enable autopilot
    mixed_commands.push(Command::set_autopilot(actor_id, true, 8000));

    // Execute mixed batch
    let mixed_responses = client.apply_batch_sync(mixed_commands, false)?;

    assert_eq!(
        mixed_responses.len(),
        3,
        "Should have response for each command"
    );

    thread::sleep(Duration::from_millis(100));

    // Cleanup
    client.apply_batch_sync(vec![Command::destroy_actor(actor_id)], false)?;

    Ok(())
}