test(m3b): headless golden tests via m3b match source abstraction
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
/// Everything the renderer needs from a match.
|
||||
///
|
||||
/// Implemented by [NativeMatch] (real Rust simulation) and by a fake in tests,
|
||||
/// so the renderer can be verified headlessly (golden tests) without the
|
||||
/// Android shared library.
|
||||
abstract class MatchSource {
|
||||
/// Allows subclasses to inherit the default getters below.
|
||||
const MatchSource();
|
||||
|
||||
/// 0 = aiming, 1 = flying, 2 = finished.
|
||||
int get phase;
|
||||
|
||||
/// Player whose turn it is.
|
||||
int get current;
|
||||
|
||||
/// Wind in units -8..8.
|
||||
int get wind;
|
||||
|
||||
/// Turn number starting at 1.
|
||||
int get turn;
|
||||
|
||||
/// 0/1 winner, 255 draw, 254 running.
|
||||
int get winner;
|
||||
|
||||
bool get isAiming => phase == 0;
|
||||
bool get isFlying => phase == 1;
|
||||
bool get isFinished => phase == 2;
|
||||
|
||||
int tankX(int player);
|
||||
int tankY(int player);
|
||||
int tankHp(int player);
|
||||
|
||||
/// Bumped whenever the terrain changed.
|
||||
int get terrainVersion;
|
||||
|
||||
/// Heightmap samples in world pixels.
|
||||
Uint16List terrain();
|
||||
|
||||
({int x, int y})? projectile();
|
||||
({int x, int y}) lastImpact();
|
||||
|
||||
int fire({required int angleMdeg, required int powerQ16, int weapon});
|
||||
int endTurn();
|
||||
int tick(int ticks);
|
||||
void dispose();
|
||||
}
|
||||
@@ -3,6 +3,8 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import 'match_source.dart';
|
||||
|
||||
/// Raw C signatures of the Rust match API (see crates/ffi/src/match_api.rs).
|
||||
typedef _NewMatchC = Pointer<Void> Function(Uint64);
|
||||
typedef _NewMatchDart = Pointer<Void> Function(int);
|
||||
@@ -55,7 +57,7 @@ typedef _ImpactDart = int Function(Pointer<Void>, Pointer<Int32>);
|
||||
/// Thin typed wrapper around the Rust match handle.
|
||||
///
|
||||
/// The handle owns the simulation; Dart only pulls snapshots for rendering.
|
||||
class NativeMatch {
|
||||
class NativeMatch extends MatchSource {
|
||||
NativeMatch(int seed) : _handle = _newMatch(seed);
|
||||
|
||||
final Pointer<Void> _handle;
|
||||
@@ -102,45 +104,55 @@ class NativeMatch {
|
||||
_lib.lookupFunction<_ImpactC, _ImpactDart>('iron_arc_last_impact');
|
||||
|
||||
/// Phase: 0 aiming, 1 flying, 2 finished.
|
||||
@override
|
||||
int get phase => _phase(_handle);
|
||||
|
||||
bool get isAiming => phase == 0;
|
||||
bool get isFlying => phase == 1;
|
||||
bool get isFinished => phase == 2;
|
||||
|
||||
/// Active player index.
|
||||
@override
|
||||
int get current => _current(_handle);
|
||||
|
||||
/// Wind in units -8..8.
|
||||
@override
|
||||
int get wind => _wind(_handle);
|
||||
|
||||
/// Turn number starting at 1.
|
||||
@override
|
||||
int get turn => _turn(_handle);
|
||||
|
||||
/// 0/1 winner, 255 draw, 254 still running.
|
||||
@override
|
||||
int get winner => _winner(_handle);
|
||||
|
||||
@override
|
||||
int tankX(int player) => _tankX(_handle, player);
|
||||
@override
|
||||
int tankY(int player) => _tankY(_handle, player);
|
||||
@override
|
||||
int tankHp(int player) => _tankHp(_handle, player);
|
||||
|
||||
/// Fires a shot. Returns 0 on success.
|
||||
@override
|
||||
int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) =>
|
||||
_fire(_handle, angleMdeg, powerQ16, weapon);
|
||||
|
||||
@override
|
||||
int endTurn() => _endTurn(_handle);
|
||||
|
||||
/// Advances the simulation, returns the number of impacts.
|
||||
@override
|
||||
int tick(int ticks) => _tick(_handle, ticks);
|
||||
|
||||
/// Bumped whenever the terrain changed.
|
||||
@override
|
||||
int get terrainVersion => _terrainVersion(_handle);
|
||||
|
||||
/// Live view into the Rust heightmap (valid until [dispose]).
|
||||
@override
|
||||
Uint16List terrain() =>
|
||||
_terrainPtr(_handle).asTypedList(_terrainLen());
|
||||
|
||||
/// Projectile position in world pixels, or null if none is in flight.
|
||||
@override
|
||||
({int x, int y})? projectile() {
|
||||
final out = calloc<Int32>(2);
|
||||
try {
|
||||
@@ -153,6 +165,7 @@ class NativeMatch {
|
||||
}
|
||||
|
||||
/// World position of the most recent impact.
|
||||
@override
|
||||
({int x, int y}) lastImpact() {
|
||||
final out = calloc<Int32>(2);
|
||||
try {
|
||||
@@ -163,6 +176,7 @@ class NativeMatch {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
|
||||
Reference in New Issue
Block a user