Skip to main content

ballistics_engine/
ffi.rs

1//! FFI bindings for iOS/Swift integration
2
3use crate::{
4    calculate_zero_angle_with_conditions, run_monte_carlo, AtmosphericConditions, BallisticInputs,
5    DragModel, MonteCarloParams, TrajectorySolver, WindConditions,
6};
7use std::os::raw::{c_char, c_double, c_int};
8use std::ptr;
9
10// FFI-safe structures with C-compatible layouts
11
12#[repr(C)]
13pub struct FFIBallisticInputs {
14    pub muzzle_velocity: c_double,         // m/s
15    pub muzzle_angle: c_double,            // radians (launch angle)
16    pub bc_value: c_double,                // ballistic coefficient
17    pub bullet_mass: c_double,             // kg
18    pub bullet_diameter: c_double,         // meters
19    pub bc_type: c_int,                    // 0=G1, 1=G7, etc.
20    pub sight_height: c_double,            // meters
21    pub target_distance: c_double,         // meters
22    pub temperature: c_double,             // Celsius
23    pub twist_rate: c_double,              // inches per turn
24    pub is_twist_right: c_int,             // 0=false, 1=true
25    pub shooting_angle: c_double,          // uphill/downhill angle in radians
26    pub altitude: c_double,                // meters
27    pub latitude: c_double,                // degrees (use NAN if not provided)
28    pub azimuth_angle: c_double,           // horizontal aiming angle in radians
29    pub use_rk4: c_int,                    // 0=Euler, 1=RK4
30    pub use_adaptive_rk45: c_int,          // 0=false, 1=true (adaptive RK45)
31    pub enable_wind_shear: c_int,          // 0=false, 1=true
32    pub enable_trajectory_sampling: c_int, // 0=false, 1=true
33    pub sample_interval: c_double,         // meters
34    pub enable_pitch_damping: c_int,       // 0=false, 1=true
35    pub enable_precession_nutation: c_int, // 0=false, 1=true
36    pub enable_spin_drift: c_int,          // 0=false, 1=true
37    pub enable_magnus: c_int,              // 0=false, 1=true
38    pub enable_coriolis: c_int,            // 0=false, 1=true
39    // Appended (keeps existing field offsets): compass bearing the shot is fired
40    // along, radians, 0=North, PI/2=East. Drives the Coriolis Eötvös/drift azimuth.
41    // Distinct from azimuth_angle (the small aiming offset). 0.0 if unset.
42    pub shot_azimuth: c_double,
43}
44
45#[repr(C)]
46pub struct FFIWindConditions {
47    pub speed: c_double, // m/s
48    // radians, wind-FROM convention: 0 = headwind, PI/2 = from the right,
49    // PI = tailwind, 3*PI/2 = from the left (matches WindConditions / WindSock).
50    pub direction: c_double,
51}
52
53#[repr(C)]
54pub struct FFIAtmosphericConditions {
55    pub temperature: c_double, // Celsius
56    pub pressure: c_double,    // hPa
57    pub humidity: c_double,    // percentage (0-100)
58    pub altitude: c_double,    // meters
59}
60
61#[repr(C)]
62pub struct FFITrajectorySample {
63    pub distance: c_double,       // meters
64    pub time: c_double,           // seconds
65    pub velocity_mps: c_double,   // meters per second
66    pub energy_joules: c_double,  // joules
67    pub drop_meters: c_double,    // meters
68    pub windage_meters: c_double, // meters
69    pub mach: c_double,           // Mach number
70    pub spin_rate_rps: c_double,  // revolutions per second
71}
72
73#[repr(C)]
74pub struct FFITrajectoryPoint {
75    pub time: c_double,
76    pub position_x: c_double,
77    pub position_y: c_double,
78    pub position_z: c_double,
79    pub velocity_magnitude: c_double,
80    pub kinetic_energy: c_double,
81}
82
83#[repr(C)]
84pub struct FFITrajectoryResult {
85    pub max_range: c_double,
86    pub max_height: c_double,
87    pub time_of_flight: c_double,
88    pub impact_velocity: c_double,
89    pub impact_energy: c_double,
90    pub points: *mut FFITrajectoryPoint,
91    pub point_count: c_int,
92    pub sampled_points: *mut FFITrajectorySample,
93    pub sampled_point_count: c_int,
94    pub min_pitch_damping: c_double,    // NAN if not calculated
95    pub transonic_mach: c_double,       // NAN if not reached
96    pub final_pitch_angle: c_double,    // NAN if not calculated
97    pub final_yaw_angle: c_double,      // NAN if not calculated
98    pub max_yaw_angle: c_double,        // NAN if not calculated
99    pub max_precession_angle: c_double, // NAN if not calculated
100}
101
102// Monte Carlo simulation parameters
103#[repr(C)]
104pub struct FFIMonteCarloParams {
105    pub num_simulations: c_int,
106    pub velocity_std_dev: c_double,
107    pub angle_std_dev: c_double,
108    pub bc_std_dev: c_double,
109    pub wind_speed_std_dev: c_double,
110    pub target_distance: c_double,     // Use NAN if not specified
111    pub base_wind_speed: c_double,     // Base wind speed in m/s
112    pub base_wind_direction: c_double, // Base wind direction in radians
113    pub azimuth_std_dev: c_double,     // Horizontal aiming variation in radians
114}
115
116// Monte Carlo simulation results
117#[repr(C)]
118pub struct FFIMonteCarloResults {
119    pub ranges: *mut c_double,
120    pub impact_velocities: *mut c_double,
121    pub impact_positions_x: *mut c_double,
122    pub impact_positions_y: *mut c_double,
123    pub impact_positions_z: *mut c_double,
124    pub num_results: c_int,
125    pub mean_range: c_double,
126    pub std_dev_range: c_double,
127    pub mean_impact_velocity: c_double,
128    pub std_dev_impact_velocity: c_double,
129    pub hit_probability: c_double, // If target_distance was specified
130}
131
132// Helper function to convert FFI inputs to internal types
133fn convert_inputs(inputs: &FFIBallisticInputs) -> BallisticInputs {
134    let mut ballistic_inputs = BallisticInputs::default();
135
136    ballistic_inputs.muzzle_velocity = inputs.muzzle_velocity;
137    ballistic_inputs.muzzle_angle = inputs.muzzle_angle;
138    ballistic_inputs.azimuth_angle = inputs.azimuth_angle;
139    ballistic_inputs.shot_azimuth = inputs.shot_azimuth;
140    ballistic_inputs.use_rk4 = inputs.use_rk4 != 0;
141    ballistic_inputs.use_adaptive_rk45 = inputs.use_adaptive_rk45 != 0;
142    ballistic_inputs.bc_value = inputs.bc_value;
143    ballistic_inputs.bullet_mass = inputs.bullet_mass;
144    ballistic_inputs.bullet_diameter = inputs.bullet_diameter;
145    ballistic_inputs.bc_type = match inputs.bc_type {
146        1 => DragModel::G7,
147        2 => DragModel::G2,
148        3 => DragModel::G5,
149        4 => DragModel::G6,
150        5 => DragModel::G8,
151        6 => DragModel::GI,
152        7 => DragModel::GS,
153        _ => DragModel::G1,
154    };
155    ballistic_inputs.sight_height = inputs.sight_height;
156    ballistic_inputs.target_distance = inputs.target_distance;
157    ballistic_inputs.temperature = inputs.temperature;
158    ballistic_inputs.twist_rate = inputs.twist_rate;
159    ballistic_inputs.is_twist_right = inputs.is_twist_right != 0;
160    ballistic_inputs.shooting_angle = inputs.shooting_angle;
161    ballistic_inputs.altitude = inputs.altitude;
162
163    if !inputs.latitude.is_nan() {
164        ballistic_inputs.latitude = Some(inputs.latitude);
165    }
166
167    // Set derived values
168    ballistic_inputs.caliber_inches = inputs.bullet_diameter / 0.0254;
169    ballistic_inputs.weight_grains = inputs.bullet_mass / 0.00006479891;
170    ballistic_inputs.bullet_length = inputs.bullet_diameter * 4.5; // match the CLI 4.5-cal default
171
172    // New advanced physics flags
173    ballistic_inputs.enable_wind_shear = inputs.enable_wind_shear != 0;
174    ballistic_inputs.enable_trajectory_sampling = inputs.enable_trajectory_sampling != 0;
175    ballistic_inputs.sample_interval = inputs.sample_interval;
176    ballistic_inputs.enable_pitch_damping = inputs.enable_pitch_damping != 0;
177    ballistic_inputs.enable_precession_nutation = inputs.enable_precession_nutation != 0;
178    ballistic_inputs.use_enhanced_spin_drift = inputs.enable_spin_drift != 0;
179    ballistic_inputs.enable_advanced_effects =
180        inputs.enable_magnus != 0 || inputs.enable_coriolis != 0;
181    // Gate Magnus and Coriolis independently so enabling one does not enable the other.
182    ballistic_inputs.enable_magnus = inputs.enable_magnus != 0;
183    ballistic_inputs.enable_coriolis = inputs.enable_coriolis != 0;
184
185    ballistic_inputs
186}
187
188// Main trajectory calculation function for FFI
189#[no_mangle]
190pub extern "C" fn ballistics_calculate_trajectory(
191    inputs: *const FFIBallisticInputs,
192    wind: *const FFIWindConditions,
193    atmosphere: *const FFIAtmosphericConditions,
194    max_range: c_double,
195    step_size: c_double,
196) -> *mut FFITrajectoryResult {
197    if inputs.is_null() {
198        return ptr::null_mut();
199    }
200
201    let inputs = unsafe { &*inputs };
202    let ballistic_inputs = convert_inputs(inputs);
203    let twist_rate_in = ballistic_inputs.twist_rate;
204
205    let wind_conditions = if wind.is_null() {
206        WindConditions::default()
207    } else {
208        let wind = unsafe { &*wind };
209        WindConditions {
210            speed: wind.speed,
211            direction: wind.direction,
212        }
213    };
214
215    let atmospheric_conditions = if atmosphere.is_null() {
216        AtmosphericConditions::default()
217    } else {
218        let atmo = unsafe { &*atmosphere };
219        AtmosphericConditions {
220            temperature: atmo.temperature,
221            pressure: atmo.pressure,
222            humidity: atmo.humidity,
223            altitude: atmo.altitude,
224        }
225    };
226
227    // Create solver and calculate trajectory
228    let (sample_temp_c, sample_pressure_hpa) = crate::atmosphere::resolve_station_conditions(
229        atmospheric_conditions.temperature,
230        atmospheric_conditions.pressure,
231        atmospheric_conditions.altitude,
232    );
233    let (_, sample_speed_of_sound) = crate::atmosphere::calculate_atmosphere(
234        atmospheric_conditions.altitude,
235        Some(sample_temp_c),
236        Some(sample_pressure_hpa),
237        atmospheric_conditions.humidity,
238    );
239
240    let mut solver =
241        TrajectorySolver::new(ballistic_inputs, wind_conditions, atmospheric_conditions);
242
243    // Set max range and time step
244    solver.set_max_range(max_range);
245    solver.set_time_step(step_size / 1000.0); // Convert to time step
246
247    match solver.solve() {
248        Ok(result) => {
249            // Convert trajectory points to FFI format
250            let point_count = result.points.len();
251            let points = if point_count > 0 {
252                let mut ffi_points = Vec::with_capacity(point_count);
253                for (i, point) in result.points.iter().enumerate() {
254                    ffi_points.push(FFITrajectoryPoint {
255                        time: point.time,
256                        position_x: point.position[0],
257                        position_y: point.position[1],
258                        position_z: point.position[2],
259                        velocity_magnitude: point.velocity_magnitude,
260                        kinetic_energy: point.kinetic_energy,
261                    });
262
263                    // Debug: Log first, last, and every 100th point.
264                    // McCoy coordinate system: X=downrange, Y=vertical, Z=lateral.
265                    // Raw position_x/_y/_z exported above are McCoy-ordered (X=downrange).
266                    #[cfg(debug_assertions)]
267                    if i == 0 || i == result.points.len() - 1 || i % 100 == 0 {
268                        eprintln!(
269                            "FFI point {}: lateral={:.2}m, vertical={:.2}m, downrange={:.2}m",
270                            i, point.position[2], point.position[1], point.position[0]
271                        );
272                    }
273                }
274                let points_ptr = ffi_points.as_mut_ptr();
275                std::mem::forget(ffi_points); // Prevent deallocation
276                points_ptr
277            } else {
278                ptr::null_mut()
279            };
280
281            // Convert sampled points if available
282            let (sampled_points, sampled_point_count) =
283                if let Some(ref samples) = result.sampled_points {
284                    let mut ffi_samples = Vec::with_capacity(samples.len());
285                    for sample in samples {
286                        ffi_samples.push(FFITrajectorySample {
287                            distance: sample.distance_m,
288                            time: sample.time_s,
289                            velocity_mps: sample.velocity_mps,
290                            energy_joules: sample.energy_j,
291                            drop_meters: sample.drop_m,
292                            windage_meters: sample.wind_drift_m,
293                            mach: if sample_speed_of_sound > 0.0 {
294                                sample.velocity_mps / sample_speed_of_sound
295                            } else {
296                                0.0
297                            },
298                            spin_rate_rps: if twist_rate_in > 0.0 {
299                                sample.velocity_mps / (twist_rate_in * 0.0254)
300                            } else {
301                                0.0
302                            },
303                        });
304                    }
305                    let count = ffi_samples.len() as c_int;
306                    let samples_ptr = ffi_samples.as_mut_ptr();
307                    std::mem::forget(ffi_samples);
308                    (samples_ptr, count)
309                } else {
310                    (ptr::null_mut(), 0)
311                };
312
313            // Extract angular state values if available
314            let (final_pitch, final_yaw, max_yaw, max_prec) =
315                if let Some(ref angular) = result.angular_state {
316                    (
317                        angular.pitch_angle,
318                        angular.yaw_angle,
319                        result.max_yaw_angle.unwrap_or(std::f64::NAN),
320                        result.max_precession_angle.unwrap_or(std::f64::NAN),
321                    )
322                } else {
323                    (std::f64::NAN, std::f64::NAN, std::f64::NAN, std::f64::NAN)
324                };
325
326            // Create result on heap
327            let ffi_result = Box::new(FFITrajectoryResult {
328                max_range: result.max_range,
329                max_height: result.max_height,
330                time_of_flight: result.time_of_flight,
331                impact_velocity: result.impact_velocity,
332                impact_energy: result.impact_energy,
333                points,
334                point_count: point_count as c_int,
335                sampled_points,
336                sampled_point_count,
337                min_pitch_damping: result.min_pitch_damping.unwrap_or(std::f64::NAN),
338                transonic_mach: result.transonic_mach.unwrap_or(std::f64::NAN),
339                final_pitch_angle: final_pitch,
340                final_yaw_angle: final_yaw,
341                max_yaw_angle: max_yaw,
342                max_precession_angle: max_prec,
343            });
344
345            Box::into_raw(ffi_result)
346        }
347        Err(_) => ptr::null_mut(),
348    }
349}
350
351// Free allocated trajectory result
352#[no_mangle]
353pub extern "C" fn ballistics_free_trajectory_result(result: *mut FFITrajectoryResult) {
354    if !result.is_null() {
355        unsafe {
356            let result = Box::from_raw(result);
357            if !result.points.is_null() && result.point_count > 0 {
358                let points = Vec::from_raw_parts(
359                    result.points,
360                    result.point_count as usize,
361                    result.point_count as usize,
362                );
363                drop(points);
364            }
365            if !result.sampled_points.is_null() && result.sampled_point_count > 0 {
366                let samples = Vec::from_raw_parts(
367                    result.sampled_points,
368                    result.sampled_point_count as usize,
369                    result.sampled_point_count as usize,
370                );
371                drop(samples);
372            }
373            drop(result);
374        }
375    }
376}
377
378// Calculate zero angle for a given target distance
379#[no_mangle]
380pub extern "C" fn ballistics_calculate_zero_angle(
381    inputs: *const FFIBallisticInputs,
382    wind: *const FFIWindConditions,
383    atmosphere: *const FFIAtmosphericConditions,
384    zero_distance: c_double,
385) -> c_double {
386    if inputs.is_null() {
387        return f64::NAN;
388    }
389
390    let inputs = unsafe { &*inputs };
391    let ballistic_inputs = convert_inputs(inputs);
392
393    let wind_conditions = if wind.is_null() {
394        WindConditions::default()
395    } else {
396        let wind = unsafe { &*wind };
397        WindConditions {
398            speed: wind.speed,
399            direction: wind.direction,
400        }
401    };
402
403    let atmospheric_conditions = if atmosphere.is_null() {
404        AtmosphericConditions::default()
405    } else {
406        let atmo = unsafe { &*atmosphere };
407        AtmosphericConditions {
408            temperature: atmo.temperature,
409            pressure: atmo.pressure,
410            humidity: atmo.humidity,
411            altitude: atmo.altitude,
412        }
413    };
414
415    // For zero angle, we want the bullet to hit at sight height at the zero distance
416    // This means the bullet crosses the line of sight at the zero distance
417    let target_height = ballistic_inputs.sight_height;
418
419    #[cfg(debug_assertions)]
420    {
421        eprintln!("FFI: Calculating zero angle for:");
422        eprintln!("  Zero distance: {} m", zero_distance);
423        eprintln!("  Target height: {} m", target_height);
424        eprintln!("  Sight height: {} m", ballistic_inputs.sight_height);
425        eprintln!("  Wind speed: {} m/s", wind_conditions.speed);
426        eprintln!("  Temperature: {} C", atmospheric_conditions.temperature);
427    }
428
429    match calculate_zero_angle_with_conditions(
430        ballistic_inputs,
431        zero_distance,
432        target_height,
433        wind_conditions,
434        atmospheric_conditions,
435    ) {
436        Ok(angle) => {
437            #[cfg(debug_assertions)]
438            eprintln!(
439                "  Calculated angle: {} rad ({} deg)",
440                angle,
441                angle * 180.0 / std::f64::consts::PI
442            );
443            angle
444        }
445        Err(e) => {
446            #[cfg(debug_assertions)]
447            eprintln!("  Error: {:?}", e);
448            f64::NAN
449        }
450    }
451}
452
453// Simple trajectory calculation for quick results
454#[no_mangle]
455pub extern "C" fn ballistics_quick_trajectory(
456    muzzle_velocity: c_double,
457    bc: c_double,
458    sight_height: c_double,
459    zero_distance: c_double,
460    target_distance: c_double,
461) -> c_double {
462    // This provides a simple drop calculation at target distance
463    // Using simplified ballistic calculations
464
465    let mut inputs = BallisticInputs::default();
466    inputs.muzzle_velocity = muzzle_velocity;
467    inputs.bc_value = bc;
468    inputs.sight_height = sight_height;
469    inputs.target_distance = target_distance;
470
471    let wind = WindConditions::default();
472    let atmo = AtmosphericConditions::default();
473
474    // First calculate zero angle
475    let zero_angle = match calculate_zero_angle_with_conditions(
476        inputs.clone(),
477        zero_distance,
478        sight_height,
479        wind.clone(),
480        atmo.clone(),
481    ) {
482        Ok(angle) => angle,
483        Err(_) => return f64::NAN,
484    };
485
486    // Now calculate trajectory with that zero angle
487    inputs.muzzle_angle = zero_angle;
488
489    let mut solver = TrajectorySolver::new(inputs, wind, atmo);
490    solver.set_max_range(target_distance * 1.1);
491
492    match solver.solve() {
493        Ok(result) => {
494            // Find the drop at target distance
495            for point in result.points {
496                if point.position[0] >= target_distance {
497                    return sight_height - point.position[1];
498                }
499            }
500            f64::NAN
501        }
502        Err(_) => f64::NAN,
503    }
504}
505
506// Monte Carlo simulation
507#[no_mangle]
508pub extern "C" fn ballistics_monte_carlo(
509    inputs: *const FFIBallisticInputs,
510    atmosphere: *const FFIAtmosphericConditions,
511    params: *const FFIMonteCarloParams,
512) -> *mut FFIMonteCarloResults {
513    if inputs.is_null() || params.is_null() {
514        return ptr::null_mut();
515    }
516
517    let inputs = unsafe { &*inputs };
518    let params = unsafe { &*params };
519
520    // Reject an out-of-range simulation count. num_simulations is a c_int (i32) cast straight to
521    // usize: a negative value would wrap to a near-max usize, and even a large positive value (up
522    // to i32::MAX ~ 2.1e9) would drive billions of iterations with the result arrays scaling to
523    // match — an unbounded-loop / OOM DoS from a single FFI call. Bound it to a sane maximum.
524    // (n == 0 also yields NaN stats and a zero-size allocation.)
525    const MAX_SIMULATIONS: c_int = 1_000_000;
526    if params.num_simulations <= 0 || params.num_simulations > MAX_SIMULATIONS {
527        return ptr::null_mut();
528    }
529
530    // Convert FFI inputs to internal types
531    let mut ballistic_inputs = convert_inputs(inputs);
532    ballistic_inputs.muzzle_height = 1.5;
533    ballistic_inputs.ground_threshold = 0.0;
534    if !atmosphere.is_null() {
535        let atmo = unsafe { &*atmosphere };
536        ballistic_inputs.temperature = atmo.temperature;
537        ballistic_inputs.pressure = atmo.pressure;
538        ballistic_inputs.humidity = (atmo.humidity / 100.0).clamp(0.0, 1.0);
539        ballistic_inputs.altitude = atmo.altitude;
540    }
541
542    // Create Monte Carlo parameters
543    let mc_params = MonteCarloParams {
544        num_simulations: params.num_simulations as usize,
545        velocity_std_dev: params.velocity_std_dev,
546        angle_std_dev: params.angle_std_dev,
547        bc_std_dev: params.bc_std_dev,
548        wind_speed_std_dev: params.wind_speed_std_dev,
549        target_distance: if params.target_distance.is_nan() {
550            None
551        } else {
552            Some(params.target_distance)
553        },
554        base_wind_speed: params.base_wind_speed,
555        base_wind_direction: params.base_wind_direction,
556        azimuth_std_dev: params.azimuth_std_dev,
557    };
558
559    // Run Monte Carlo simulation
560    match run_monte_carlo(ballistic_inputs, mc_params) {
561        Ok(results) => {
562            let num_results = results.ranges.len() as c_int;
563
564            // Calculate statistics
565            let mean_range: f64 = results.ranges.iter().sum::<f64>() / num_results as f64;
566            let variance_range: f64 = results
567                .ranges
568                .iter()
569                .map(|r| (r - mean_range).powi(2))
570                .sum::<f64>()
571                / num_results as f64;
572            let std_dev_range = variance_range.sqrt();
573
574            let mean_velocity: f64 =
575                results.impact_velocities.iter().sum::<f64>() / num_results as f64;
576            let variance_velocity: f64 = results
577                .impact_velocities
578                .iter()
579                .map(|v| (v - mean_velocity).powi(2))
580                .sum::<f64>()
581                / num_results as f64;
582            let std_dev_velocity = variance_velocity.sqrt();
583
584            // Calculate hit probability if target distance was specified. MBA-971: use the shared
585            // position-based criterion (fraction within DEFAULT_HIT_RADIUS_M of the point of aim
586            // at the target plane). The old inline version had a redundant `distance < target`
587            // clause comparing a ~meter deviation to the ~hundreds-of-meters target distance
588            // (effectively always true), and the CLI used a different range-based notion entirely.
589            let hit_probability = if params.target_distance.is_nan() {
590                0.0
591            } else {
592                results.hit_probability(crate::DEFAULT_HIT_RADIUS_M)
593            };
594
595            // Allocate memory for arrays
596            let ranges_ptr = unsafe {
597                let ptr = std::alloc::alloc(
598                    std::alloc::Layout::array::<c_double>(num_results as usize).unwrap(),
599                ) as *mut c_double;
600                for (i, &range) in results.ranges.iter().enumerate() {
601                    *ptr.add(i) = range;
602                }
603                ptr
604            };
605
606            let velocities_ptr = unsafe {
607                let ptr = std::alloc::alloc(
608                    std::alloc::Layout::array::<c_double>(num_results as usize).unwrap(),
609                ) as *mut c_double;
610                for (i, &vel) in results.impact_velocities.iter().enumerate() {
611                    *ptr.add(i) = vel;
612                }
613                ptr
614            };
615
616            let pos_x_ptr = unsafe {
617                let ptr = std::alloc::alloc(
618                    std::alloc::Layout::array::<c_double>(num_results as usize).unwrap(),
619                ) as *mut c_double;
620                for (i, pos) in results.impact_positions.iter().enumerate() {
621                    *ptr.add(i) = pos.x;
622                }
623                ptr
624            };
625
626            let pos_y_ptr = unsafe {
627                let ptr = std::alloc::alloc(
628                    std::alloc::Layout::array::<c_double>(num_results as usize).unwrap(),
629                ) as *mut c_double;
630                for (i, pos) in results.impact_positions.iter().enumerate() {
631                    *ptr.add(i) = pos.y;
632                }
633                ptr
634            };
635
636            let pos_z_ptr = unsafe {
637                let ptr = std::alloc::alloc(
638                    std::alloc::Layout::array::<c_double>(num_results as usize).unwrap(),
639                ) as *mut c_double;
640                for (i, pos) in results.impact_positions.iter().enumerate() {
641                    *ptr.add(i) = pos.z;
642                }
643                ptr
644            };
645
646            // Create result structure
647            let result = Box::new(FFIMonteCarloResults {
648                ranges: ranges_ptr,
649                impact_velocities: velocities_ptr,
650                impact_positions_x: pos_x_ptr,
651                impact_positions_y: pos_y_ptr,
652                impact_positions_z: pos_z_ptr,
653                num_results,
654                mean_range,
655                std_dev_range,
656                mean_impact_velocity: mean_velocity,
657                std_dev_impact_velocity: std_dev_velocity,
658                hit_probability,
659            });
660
661            Box::into_raw(result)
662        }
663        Err(_) => ptr::null_mut(),
664    }
665}
666
667// Free Monte Carlo results
668#[no_mangle]
669pub extern "C" fn ballistics_free_monte_carlo_results(results: *mut FFIMonteCarloResults) {
670    if results.is_null() {
671        return;
672    }
673
674    unsafe {
675        let results = Box::from_raw(results);
676        let num = results.num_results as usize;
677
678        // Free arrays
679        if !results.ranges.is_null() {
680            std::alloc::dealloc(
681                results.ranges as *mut u8,
682                std::alloc::Layout::array::<c_double>(num).unwrap(),
683            );
684        }
685
686        if !results.impact_velocities.is_null() {
687            std::alloc::dealloc(
688                results.impact_velocities as *mut u8,
689                std::alloc::Layout::array::<c_double>(num).unwrap(),
690            );
691        }
692
693        if !results.impact_positions_x.is_null() {
694            std::alloc::dealloc(
695                results.impact_positions_x as *mut u8,
696                std::alloc::Layout::array::<c_double>(num).unwrap(),
697            );
698        }
699
700        if !results.impact_positions_y.is_null() {
701            std::alloc::dealloc(
702                results.impact_positions_y as *mut u8,
703                std::alloc::Layout::array::<c_double>(num).unwrap(),
704            );
705        }
706
707        if !results.impact_positions_z.is_null() {
708            std::alloc::dealloc(
709                results.impact_positions_z as *mut u8,
710                std::alloc::Layout::array::<c_double>(num).unwrap(),
711            );
712        }
713
714        // Box automatically deallocates the result structure
715    }
716}
717
718// Get library version
719#[no_mangle]
720pub extern "C" fn ballistics_get_version() -> *const c_char {
721    // Return a pointer to a static NUL-terminated string (the caller must NOT free it).
722    // Previously this leaked a freshly-allocated CString on every call and reported a
723    // stale hardcoded "0.3.0"; use the real crate version with no allocation.
724    concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr() as *const c_char
725}