Files
EscapistsReintroduced/tests/test_powered_flight_policy.py
T

64 lines
3.5 KiB
Python

"""Run actual extracted fixture rules; no native flight or Enforce claims."""
from pathlib import Path
import unittest
from policy_harness import body, run
SOURCE=Path(__file__).resolve().parents[1]/'addons/core/Scripts/Game/ERR/ERR_NativePoweredFlightProbe.c'
class PoweredFlightPolicy(unittest.TestCase):
def test_actual_authority_all_inputs(self):
method=body(SOURCE,'static bool Authority(')
run('#include <cassert>\n'+method+'''
int main(){for(int mask=0;mask<256;mask++) {
bool server=mask&1, enabled=mask&2, stopped=mask&4, pilot=mask&8, seat=mask&16, player=mask&32, alive=mask&64, fuel=mask&128;
assert(Authority(server,enabled,stopped,pilot,seat,player,alive,fuel)==(server&&enabled&&!stopped&&pilot&&seat&&!player&&alive&&fuel));
}}
''')
def test_actual_hover_landing_and_bounded_input_rules(self):
methods='\n'.join(body(SOURCE,key).replace('Math.','Math::') for key in ['static float ClampInput(', 'static float DesiredVertical(', 'static bool Hover(', 'static bool Landed('])
run('''
#include <cassert>
#include <cmath>
#include <algorithm>
struct Math { static float Clamp(float x,float low,float high){return std::clamp(x,low,high);} static float AbsFloat(float x){return std::abs(x);} };
'''+methods+'''
int main(){
assert(ClampInput(3)==1 && ClampInput(-3)==-1);
assert(ClampInput(0.8f,0.15f)==0.15f);
assert(DesiredVertical(0,false)==1.5f && DesiredVertical(8,false)==0);
assert(DesiredVertical(8,true)==-0.8f && DesiredVertical(0,true)==0);
assert(Hover(8,0.2f,0.1f,1,1));
assert(!Hover(0,0,0,1,0) && !Hover(11,0,0,1,0));
assert(!Hover(8,2,0,1,0) && !Hover(8,0,0.6f,1,0));
assert(!Hover(8,0,0,0.9f,0) && !Hover(8,0,0,1,9));
assert(Landed(true,0.1f,0.1f,0.01f,1));
assert(!Landed(false,0,0,0,1)); // low stationary hover is never landing
assert(!Landed(true,2,0,0,1) && !Landed(true,0,1,0,1));
assert(!Landed(true,0,0,0.2f,1) && !Landed(true,0,0,0,0.8f));
}
''')
def test_native_input_only_and_local_hooks(self):
source=SOURCE.read_text()
for forbidden in ('SetVelocity(', 'SetAngularVelocity(', 'SetWorldTransform(', 'SetOrigin(', 'SetAngles(', '.Teleport(', 'ForceStartEngine(', '.EngineStart(', 'RotorSetForce', 'RotorSetTorque', 'SetHealth(', 'SetFuel(', 'SetGlobalFuelConsumptionScale', 'GetInputManager('):
self.assertNotIn(forbidden,source)
self.assertTrue(source.startswith('#ifdef WORKBENCH'))
self.assertIn('outcome == "powered-flight"',source)
self.assertIn('controller != m_Character || owner != m_Pilot',source)
self.assertIn('controller != m_Controller',source)
self.assertIn('super.OnPrepareControls(owner, am, dt, player)',source)
self.assertEqual(source.count('.StartEngine()'),1)
self.assertEqual(source.count('physics.SetActive('),1)
self.assertIn('m_Wakes == 0',source)
for control in ('Collective','CyclicForward','CyclicAside','AntiTorque'):
self.assertIn('m_Input.SetVehicle'+control+'(',source)
self.assertIn('m_Input.GetVehicle'+control+'()',source)
def test_stop_and_engine_shutdown_are_bounded(self):
source=SOURCE.read_text();stop=body(SOURCE,'void Stop()')
self.assertIn('OwnPilot() && !PlayerPresent()',stop)
self.assertIn('s_Current = null',stop)
self.assertNotIn('StopEngine',stop) # no engine kill on midair fail/capture
self.assertNotIn('DeleteSpawned',stop)
self.assertIn('m_Stage >= 4 && m_Stage <= 6 && !m_Controller.IsEngineOn()',source)
self.assertIn('m_Fuel.GetTotalFuel() < m_InitialFuel - 0.05',source)
self.assertIn('m_Controller.StopEngine(false)',source)
self.assertIn('ERR_PoweredFlightRules.Landed(m_Simulation.HasAnyGroundContact()',source)
if __name__=='__main__': unittest.main()