feat(m3a): playable flame client with camera, hud and device perf gate

This commit is contained in:
2026-10-24 22:54:29 +02:00
parent e77595eaf0
commit a30784479b
15 changed files with 1238 additions and 144 deletions
+26 -8
View File
@@ -1,7 +1,12 @@
//! Flutter <-> Rust bridge.
//!
//! Until the flutter_rust_bridge integration (M3) this crate exposes plain
//! C ABI functions so the Android pipeline can be verified end to end.
//! Two layers live here:
//! - stateless probe functions (ABI marker, self test) used to verify the
//! build pipeline and cross-target determinism,
//! - [`match_api`], the stateful API the Flame client drives.
#![allow(clippy::not_unsafe_ptr_arg_deref)]
pub mod match_api;
/// Magic marker to verify that the CDylib was loaded and symbols resolve.
pub const ABI_MARKER: u32 = 0x1A2B_3C4D;
@@ -13,9 +18,6 @@ pub extern "C" fn iron_arc_abi_marker() -> u32 {
}
/// Runs a fixed scripted match and returns its state hash.
///
/// The same seed must produce the same hash on every target (android arm64,
/// x86_64, wasm). This is the cross-target determinism probe from ADR 0002.
#[no_mangle]
pub extern "C" fn iron_arc_selftest(seed: u64) -> u64 {
let mut sim = sim::state::Sim::default_match(seed);
@@ -33,9 +35,7 @@ pub extern "C" fn iron_arc_selftest(seed: u64) -> u64 {
/// Returns the simulation version as a packed `major*10000 + minor*100 + patch`.
#[no_mangle]
pub extern "C" fn iron_arc_sim_version_code() -> u32 {
let mut parts = sim::SIM_VERSION
.split('.')
.filter_map(|p| p.parse::<u32>().ok());
let mut parts = sim::SIM_VERSION.split('.').filter_map(|p| p.parse::<u32>().ok());
let major = parts.next().unwrap_or(0);
let minor = parts.next().unwrap_or(0);
let patch = parts.next().unwrap_or(0);
@@ -61,4 +61,22 @@ mod tests {
fn version_code_is_packed() {
assert_eq!(iron_arc_sim_version_code(), 100);
}
#[test]
fn match_handle_roundtrip() {
unsafe {
let handle = match_api::iron_arc_new_match(7);
assert!(!handle.is_null());
assert_eq!(match_api::iron_arc_phase(handle), 0);
assert_eq!(match_api::iron_arc_current(handle), 0);
assert_eq!(match_api::iron_arc_fire(handle, 45_000, 65_536, 1), 0);
assert_eq!(match_api::iron_arc_phase(handle), 1);
let mut xy = [0i32; 2];
assert_eq!(match_api::iron_arc_projectile(handle, xy.as_mut_ptr()), 1);
let impacts = match_api::iron_arc_tick(handle, 400);
assert!(impacts >= 1, "expected an impact, got {impacts}");
assert!(match_api::iron_arc_terrain_ptr(handle).is_null() == false);
match_api::iron_arc_free_match(handle);
}
}
}
+270
View File
@@ -0,0 +1,270 @@
//! Stateful C API used by the Flutter client to run a match.
//!
//! Deliberately small and stable: the client owns the handle, calls `tick` in a
//! fixed-step loop and reads a snapshot for rendering. No data is pushed from
//! Rust; the renderer pulls.
use sim::state::{Command, Event, Phase, Sim};
/// Opaque match handle owned by Dart.
pub struct MatchHandle {
sim: Sim,
terrain_version: u32,
impacts: u32,
last_impact: [i32; 2],
}
impl MatchHandle {
fn sim(&self) -> &Sim {
&self.sim
}
fn sim_mut(&mut self) -> &mut Sim {
&mut self.sim
}
}
/// Creates a new match with default spawns. Free with [`iron_arc_free_match`].
#[no_mangle]
pub extern "C" fn iron_arc_new_match(seed: u64) -> *mut MatchHandle {
Box::into_raw(Box::new(MatchHandle {
sim: Sim::default_match(seed),
terrain_version: 0,
impacts: 0,
last_impact: [0, 0],
}))
}
/// Frees a match handle created by [`iron_arc_new_match`].
///
/// # Safety
/// `handle` must come from [`iron_arc_new_match`] and must not be used afterwards.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_free_match(handle: *mut MatchHandle) {
if !handle.is_null() {
drop(Box::from_raw(handle));
}
}
/// Submits a shot. Returns 0 on success, 1 if the command was rejected.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_fire(
handle: *mut MatchHandle,
angle_mdeg: u32,
power_q16: i32,
weapon: u8,
) -> i32 {
let Some(h) = handle.as_mut() else {
return 2;
};
match h.sim_mut().submit(Command::Fire {
angle_mdeg,
power: power_q16,
weapon,
}) {
Ok(()) => 0,
Err(_) => 1,
}
}
/// Ends the current turn without firing.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_end_turn(handle: *mut MatchHandle) -> i32 {
let Some(h) = handle.as_mut() else {
return 2;
};
match h.sim_mut().submit(Command::EndTurn) {
Ok(()) => 0,
Err(_) => 1,
}
}
/// Advances the simulation by `ticks` fixed steps. Returns the number of
/// impacts that happened (used by the client to spawn VFX).
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_tick(handle: *mut MatchHandle, ticks: u32) -> u32 {
let Some(h) = handle.as_mut() else {
return 0;
};
let mut impacts = 0;
for _ in 0..ticks {
for event in h.sim_mut().tick() {
if let Event::Impact { x, y, .. } = event {
impacts += 1;
h.last_impact = [sim::fixed::to_int(x), sim::fixed::to_int(y)];
h.terrain_version = h.terrain_version.wrapping_add(1);
}
}
}
h.impacts += impacts;
impacts
}
/// Phase: 0 = aiming, 1 = flying, 2 = finished.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_phase(handle: *const MatchHandle) -> u8 {
match handle.as_ref() {
Some(h) => match h.sim().phase {
Phase::Aiming => 0,
Phase::Flying => 1,
Phase::Finished => 2,
},
None => 3,
}
}
/// Player index whose turn it is.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_current(handle: *const MatchHandle) -> u8 {
handle.as_ref().map(|h| h.sim().current).unwrap_or(0)
}
/// Wind in units -8..=8.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_wind(handle: *const MatchHandle) -> i32 {
handle.as_ref().map(|h| h.sim().wind).unwrap_or(0)
}
/// Current turn number (1-based).
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_turn(handle: *const MatchHandle) -> u32 {
handle.as_ref().map(|h| h.sim().turn as u32).unwrap_or(0)
}
/// Winner: 0 or 1, 255 for a draw, 254 while the match is running.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_winner(handle: *const MatchHandle) -> u8 {
match handle.as_ref() {
Some(h) => match (h.sim().phase, h.sim().winner) {
(Phase::Finished, Some(w)) => w,
(Phase::Finished, None) => 255,
_ => 254,
},
None => 254,
}
}
/// Tank world x in pixels.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`]; `player` < 2.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_tank_x(handle: *const MatchHandle, player: u8) -> i32 {
handle
.as_ref()
.map(|h| sim::fixed::to_int(h.sim().tanks[(player as usize).min(1)].x))
.unwrap_or(0)
}
/// Tank world y in pixels.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`]; `player` < 2.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_tank_y(handle: *const MatchHandle, player: u8) -> i32 {
handle
.as_ref()
.map(|h| sim::fixed::to_int(h.sim().tanks[(player as usize).min(1)].y))
.unwrap_or(0)
}
/// Tank hit points.
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`]; `player` < 2.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_tank_hp(handle: *const MatchHandle, player: u8) -> i32 {
handle
.as_ref()
.map(|h| h.sim().tanks[(player as usize).min(1)].hp)
.unwrap_or(0)
}
/// Pointer to the heightmap samples (u16, `iron_arc_terrain_len()` entries).
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`]. The pointer
/// stays valid until the match is freed.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_terrain_ptr(handle: *const MatchHandle) -> *const u16 {
match handle.as_ref() {
Some(h) => h.sim().terrain.heights().as_ptr(),
None => core::ptr::null(),
}
}
/// Number of heightmap samples.
#[no_mangle]
pub extern "C" fn iron_arc_terrain_len() -> u32 {
sim::terrain::SAMPLES as u32
}
/// Increments whenever the terrain changed (renderer cache invalidation).
///
/// # Safety
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
#[no_mangle]
pub unsafe extern "C" fn iron_arc_terrain_version(handle: *const MatchHandle) -> u32 {
handle.as_ref().map(|h| h.terrain_version).unwrap_or(0)
}
/// Writes the projectile position into `out` (x, y). Returns 1 if in flight.
///
/// # Safety
/// `handle` must be valid and `out` must point to at least 2 `i32` values.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_projectile(handle: *const MatchHandle, out: *mut i32) -> i32 {
let Some(h) = handle.as_ref() else {
return 0;
};
match &h.sim().projectile {
Some(p) => {
if !out.is_null() {
*out = sim::fixed::to_int(p.x);
*out.add(1) = sim::fixed::to_int(p.y);
}
1
}
None => 0,
}
}
/// Writes the last impact position into `out` (x, y) and returns the impact count.
///
/// # Safety
/// `handle` must be valid and `out` must point to at least 2 `i32` values.
#[no_mangle]
pub unsafe extern "C" fn iron_arc_last_impact(handle: *const MatchHandle, out: *mut i32) -> u32 {
let Some(h) = handle.as_ref() else {
return 0;
};
if !out.is_null() {
*out = h.last_impact[0];
*out.add(1) = h.last_impact[1];
}
h.impacts
}
+5
View File
@@ -105,6 +105,11 @@ impl Terrain {
self.heights.is_empty()
}
/// Raw heightmap samples (read-only view, used by the renderer).
pub fn heights(&self) -> &[u16] {
&self.heights
}
/// Surface height at sample index.
pub fn height_at(&self, idx: usize) -> u16 {
self.heights[idx.min(SAMPLES - 1)]