RUST · GAMEPLAY & TACTICAL AI

defy-rs — Open-World Wanted Escalation & Tactical Pursuit AI in Pure Rust

defy-rs is a game AI library that models open-world crime witness reporting, 1-to-6 star wanted level escalation, and coordinated police pursuit tactics. It uses line-of-sight sensory cones, last-known-position heatmaps, and interceptor roadblock spawning to drive dynamic sandbox gameplay.

$ cargo add defy-rs
GitHub ↗

Wanted Level & Dispatch Simulator

Simulate crime incidents, test witness reaction latencies, silence 911 callers, and break line-of-sight during high-speed police pursuits.

Wanted Rating (Click star to override)
Crime Score
0 pts
Pursuit Status
CLEAN RECORD
RADAR: Tactical City Grid [Sector 4]
SUSPECT: Moving @ 24 m/s [Visual Contact]
SEARCH RADIUS: 180m | POLICE UNITS: 3
Active Dispatch Composition 0 Units Active
No active police response.
Roadblock Chance: 0% Spawn Interval: --
Commit Criminal Infraction
Police Radio & Event Feed
[00:00] City Dispatch: Sector quiet. Patrol units in routine sweep.

Three Core Gameplay Subsystems

Engineered from ground up with data-oriented structures, cache locality, and Bevy ECS schedule compatibility.

Wanted Level System

defy_rs::wanted

Progressive 1-to-6 stars system with score thresholds, unit compositions, dynamic roadblock probabilities, and line-of-sight cooldown decay.

  • 1 Star: Cruiser patrol with peaceful arrest attempt
  • 3 Stars: SWAT Enforcers, spikes, helicopter air support
  • 6 Stars: Military martial law, Rhino tanks, shoot-on-sight
  • Out-of-sight search radius bubble with progressive star decay

Crime & Witness Pipeline

defy_rs::crime

Multi-modal sensory detection combining directional visual FOV cones with omnidirectional acoustic sound propagation.

  • Auditory radius: gunfire (60-90m), blast explosions (200m)
  • Witness shock delay before dialing 911
  • Silenceable witnesses cancel reports before dispatch triggers
  • Categorized severities: Minor, Moderate, Felony, Capital

Bystander AI Archetypes

defy_rs::npc

Rich bystander reactions determined by psychological personality traits and proximity to gunfire, vehicle theft, and melee assaults.

  • Coward: Cowers or flees screaming upon loud sounds
  • AverageCitizen: Raises hands at gunpoint, calls 911 when safe
  • Aggressive: Fights back against melee assaults and carjackings
  • ArmedVigilante: Conceals pistol, returns fire during shootout

16 Unit Tests & Micro-Benchmarks

Every sensory check, dispatch rule, and AI transition is covered by automated integration tests.

Automated Test Matrix 16 / 16 PASSED (0.00s)

Suite Test Function Status
wantedtest_star_progression_by_scoreOK
wantedtest_dispatch_composition_per_starOK
wantedtest_line_of_sight_and_decay_cycleOK
wantedtest_reacquisition_resets_decayOK
wantedtest_deadly_force_at_high_wanted_levelOK
wantedtest_busted_arrest_mechanicOK
crimetest_crime_severities_and_scoresOK
crimetest_sensory_detection_audio_omnidirectionalOK
crimetest_sensory_detection_vision_coneOK
crimetest_911_call_pipeline_and_dispatchOK
crimetest_silence_witness_cancels_callOK
npctest_coward_cowers_on_gunfireOK
npctest_average_citizen_hands_up_at_gunpointOK
npctest_aggressive_fights_back_on_meleeOK
npctest_armed_vigilante_draws_weapon_on_gunfireOK
npctest_panic_timer_calms_downOK

Comparative Benchmarks vs Game Scripts Full Report (BENCHMARKS.md) →

Scenario defy-rs (Rust) Lua / C# Script Speedup
Police Pursuit (50 Units) 0.20 µs (4.08 ns/u) 25 – 60 µs 60× – 300×
NPC Crowd Triage (200 NPCs) 1.49 µs (7.47 ns/npc) 45 – 120 µs 13× – 80×
Witness Sensory Detection 3.10 ns (322M/s) 350 – 900 ns 40× – 290×
Star Progression & Scaling 21.55 ns (46M/s) 800 – 2,200 ns 12× – 100×
Hot Loop Heap Allocations 0 bytes 4 – 16 allocs Zero GC

Massive Crowd & Dispatch Simulation

In open-world cities with 5,000+ simultaneous NPCs and 50+ police pursuit units, defy-rs runs entire sensory sweeps and tactical coordination in under 0.05 ms per frame. Run cargo run --release --example bench_vs_original to verify.

Integrate defy-rs in Minutes

Plug directly into your Bevy ECS systems or custom engine tick.

src/main.rs
use glam::Vec3;
use defy_rs::{
    WantedLevelSystem, WantedStatus, WitnessSystem,
    CrimeEvent, CrimeType, NpcEntity, NpcPersonality, NpcContext, ThreatStimulus,
};

fn main() {
    // 1. Initialize Wanted Level System
    let mut wanted = WantedLevelSystem::default();
    wanted.add_crime_score(150, Vec3::ZERO); // 1 Star Triggered
    println!("Current Wanted Stars: {}", wanted.stars);

    // 2. Register Witness and 911 Call Countdown
    let mut witnesses = WitnessSystem::new();
    witnesses.register_witness(
        1,
        Vec3::new(5.0, 0.0, 5.0),
        CrimeEvent {
            crime_type: CrimeType::VehicleTheft,
            perpetrator_id: 100,
            location: Vec3::ZERO,
            timestamp_sec: 0.0,
            is_direct_police_view: false,
        },
        3.0, // 3.0s call timer
    );

    // Player intimidates witness before report finishes:
    let silenced = witnesses.silence_witness(1);
    assert!(silenced);

    // 3. Simulate Bystander Reaction
    let mut citizen = NpcEntity::new(10, Vec3::ZERO, NpcPersonality::AverageCitizen);
    citizen.update(&NpcContext {
        threats: vec![ThreatStimulus {
            source_id: 100,
            position: Vec3::new(0.0, 0.0, 3.0),
            is_gunfire: false,
            is_aimed_at_me: true,
            is_melee_attack: false,
        }],
        delta_time: 0.016,
    });
    println!("Citizen reaction: {:?}", citizen.reaction); // HandsUp
}