1
2#include "../Modules/Module.h"
3#include <cstdlib>
4#include <algorithm>
5#include <cmath>
6#include <iostream>
7#include <memory>
8#include <vector>
9
12static std::vector<SOUNDS> played, stopped;
13static float soundDuration = 1.0f;
14static void Check(bool condition, const char* message)
15{
16 if (!condition) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
17}
18
19
26{
28}
29float Sound::Play(
SOUNDS sound,
float) { played.push_back(sound);
return soundDuration; }
31
32struct Fixture
33{
34 Logger logger;
35 std::unique_ptr<Bus> bus = std::make_unique<Bus>(&logger);
36 std::unique_ptr<Sound> sound = std::make_unique<Sound>(nullptr);
37 RiseMath::Matrix view;
38 TCAS tcas{bus.get(), &logger, &view};
39 Vmu vmu{bus.get(), &logger, sound.get()};
40 Fixture()
41 {
42 soundDuration = 1.0f;
45 for (
auto& ship :
playerships) ship = s_network_objects{};
49 bus->RadioAltitudeKm = 1.0f;
53 Step(2.0f);
54 played.clear(); stopped.clear(); bus->vmuHold = 0;
55 }
56 void Queue(const char* name, bool test = false, float delay = 0)
57 {
59 bus->commandStream.push_back(command);
60 }
62};
63
64static void InhibitsAndResets()
65{
66 Fixture f;
67 f.bus->RadioAltitudeKm = 0.3048f; f.Step();
68 Check(!f.bus->TCASInhibited, "exactly 1,000 feet allows TCAS");
70 f.Queue("TCASClimb", false, 5); f.Queue("UnrelatedCommand");
71 f.bus->RadioAltitudeKm = 0.3047f; f.Step();
72 Check(f.bus->TCASInhibited, "below 1,000 feet inhibits TCAS");
74 "inhibition clears displayed mode and target");
75 Check(f.bus->commandStream.size() == 1 && f.bus->commandStream[0].name == "UnrelatedCommand",
76 "inhibition removes delayed stale speech and preserves unrelated commands");
77 f.bus->RadioAltitudeKm = 1;
79 Check(!f.bus->TCASInhibited, "GPWS Reset is not an alert");
81 Check(!f.bus->TCASInhibited, "GPWS NoAlert allows TCAS");
83 {
85 f.Queue("IncreaseClimb"); f.Step();
86 Check(f.bus->TCASInhibited && played.empty(), "each active GPWS alert suppresses TCAS");
87 }
88}
89
90static void VoicesAndPreemption()
91{
92 Fixture f;
94 {
97 Check(played.back() ==
SOUND_TCASTRAFFIC + i,
"Dropship aliases play correct recordings");
98 }
99 Check(
TcasAudio::Find(
"TcasSystemTest") == -1,
"test control is not mistaken for speech");
100 f.Queue("TCASClimb"); f.Step(1.1f);
102 f.Queue("GPWSPullUp"); f.Step();
103 Check(stopped.empty() && played.back() ==
SOUND_TCASCLIMB,
"GPWS lets the active TCAS phrase finish");
104 f.Step(1);
105 Check(played.back() ==
SOUND_GPWSPULLUP,
"GPWS takes the next voice slot after TCAS finishes");
107 f.bus->RadioAltitudeKm = 0; f.Step();
108 Check(stopped.empty() && played.back() ==
SOUND_TCASDESCEND,
"inhibit altitude clears queued TCAS without clipping active speech");
109 f.bus->RadioAltitudeKm = 1; f.bus->vmuHold = 0;
111 f.bus->commandStream.push_back(live); f.vmu.FrameMove(.1f);
112 const auto count = played.size(); ++f.bus->TCASRevision; f.vmu.FrameMove(.1f);
113 Check(stopped.empty() && played.size() == count, "TCAS advisory revision cannot restart the active recording");
114}
115
116static void CompleteGroundTest()
117{
118 Fixture f;
121 f.Queue("TcasSystemTest");
122 std::vector<float> times;
123 for (int i = 1; i <= 500; ++i)
124 {
125 const auto before = played.size(); f.Step();
126 if (played.size() > before)
127 {
128 times.push_back(i * 0.1f);
130 "display follows the phrase actually started by the VMU");
131 }
133 "display test never publishes a live advisory or target");
134 if (i > 1 && !f.bus->TCASTestActive) break;
135 }
141 Check(played == std::vector<SOUNDS>(std::begin(expected), std::end(expected)),
142 "ground test speaks all 15 callouts in Dropship order exactly once");
143 Check(times[0] >= 1.99f && times[10] - times[9] >= 2.99f &&
144 times[11] - times[10] >= 2.99f && times[13] - times[12] >= 1.49f,
145 "test preserves initial, maintain, adjust, and crossing pauses");
146 Check(!f.bus->TCASTestActive && f.bus->commandStream.empty(), "test ends without queued residue");
147 Check(f.bus->TCASTestCallout == -1, "completed test clears display state");
149 "test creates no real traffic threat");
150}
151
152static void CancelAndRestart()
153{
154 Fixture f;
155 f.Queue("TcasSystemTest", false, 5); f.Step(2);
156 Check(!f.bus->TCASTestActive, "delayed test command waits");
157 f.bus->commandStream[0].delay = 0; f.Step(2);
158 Check(f.bus->TCASTestActive && played.back() ==
SOUND_TCASTRAFFIC,
"test starts");
159 f.Queue("TcasSystemTest"); f.Step();
160 Check(!f.bus->TCASTestActive && stopped.back() ==
SOUND_TCASTRAFFIC,
"toggle cancels active speech");
161 Check(f.bus->TCASTestCallout == -1, "toggle clears display");
162 f.Queue("TcasSystemTest"); f.Step(2);
165 Check(!f.bus->TCASTestActive && f.bus->commandStream.empty(), "power loss cancels test and queue");
166 Check(f.bus->TCASTestCallout == -1, "power loss clears display");
168 Check(!f.bus->TCASTestActive, "power restoration does not resume stale test");
169 f.Queue("TcasSystemTest"); f.Step(2);
171 Check(!f.bus->TCASTestActive, "GPWS cancels active test");
172 Check(f.bus->TCASTestCallout == -1, "GPWS clears test display");
173 f.Queue("TcasSystemTest"); f.Step(2);
174 Check(!f.bus->TCASTestActive, "cannot start test during GPWS alert");
176 f.Queue("TcasSystemTest"); f.Step(2);
177 f.Queue("VMUTest"); f.Step();
178 Check(!f.bus->TCASTestActive, "normal VMU test cancels TCAS test");
179 Check(f.bus->TCASTestCallout == -1, "VMU test clears TCAS display");
180}
181
182static void DisplayPlaybackAndLifecycle()
183{
184 Fixture f;
185 f.Queue("TcasSystemTest"); f.Step(.1f);
186 Check(f.bus->TCASTestActive && f.bus->TCASTestCallout == -1,
187 "display starts with symbol check before first voice");
188 f.bus->vmuHold = 10;
189 f.Step(3);
190 Check(f.bus->TCASTestCallout == -1 && played.empty(), "busy audio cannot advance display ahead of speech");
191 f.bus->vmuHold = 0;
192 f.tcas.FrameMove(.1f);
193 Check(!f.bus->commandStream.empty() && f.bus->TCASTestCallout == -1,
194 "queueing a test phrase alone does not advance display");
195 f.vmu.FrameMove(.1f);
196 Check(f.bus->TCASTestCallout == 0, "consuming test speech advances display");
197 soundDuration = 4;
198 f.Step(2); f.Step();
199 Check(f.bus->TCASTestCallout == 1, "next phrase updates display");
200 f.Step(2);
201 Check(f.bus->TCASTestCallout == 1, "display holds throughout a long recording");
203 Check(!f.bus->TCASTestActive && f.bus->TCASTestCallout == -1 && f.bus->commandStream.empty(),
204 "vehicle change cancels display, test sequence and queued speech");
205 f.Step(3);
206 Check(!f.bus->TCASTestActive, "vehicle change cannot replay old test");
208 f.Queue("TcasSystemTest"); f.Step(2);
209 Check(f.bus->TCASTestActive && f.bus->TCASTestCallout == 0,
210 "cockpit avionics self-test remains available in simulator");
211 f.Queue("TcasSystemTest"); f.Step();
212 f.Queue("TcasSystemTest"); f.Step();
213 Check(f.bus->TCASTestCallout == -1, "restart clears previous phrase during initial display check");
214 f.Step(2);
215 f.bus->GPWS.testLevel = 1; f.Step();
216 Check(!f.bus->TCASTestActive && f.bus->TCASTestCallout == -1,
217 "GPWS self-test cancels the competing TCAS sound and display test");
218 f.Queue("TcasSystemTest"); f.Step(2);
219 Check(!f.bus->TCASTestActive, "TCAS test cannot cover an active GPWS self-test");
220 f.bus->GPWS.testLevel = 0; f.Step(3);
221 Check(!f.bus->TCASTestActive, "GPWS test completion does not replay cancelled TCAS test");
222}
223
224static constexpr float FT = .0003048f, FPM = FT/60;
225static s_network_objects& Traffic(Fixture& f,
int slot,
float range,
float altitudeFeet,
float rateFpm = 0,
float closing = .15f)
226{
229 t.
active = t.visible = t.isOnScanner = t.transponder =
true;
234 return t;
235}
236static void Fly(Fixture& f, float seconds, float ownFpm = 0, float frame = .1f)
237{
238 f.bus->VerticalSpeedKms = ownFpm*FPM;
239 for (float elapsed = 0; elapsed < seconds-.0001f; )
240 {
241 const float dt = (std::min)(frame, seconds-elapsed);
243 own.
position.y += ownFpm*FPM*dt; own.mslKm += ownFpm*FPM*dt;
246 {
249 }
250 f.Step(dt); elapsed += dt;
251 }
252}
253static bool Heard(
SOUNDS sound) {
return std::find(played.begin(), played.end(), sound) != played.end(); }
254static void LiveAdvisoryRegression()
255{
256 Fixture f; Traffic(f, 1, 6, -100);
257 Fly(f, 20);
259 Check(!Heard(
SOUND_TCASMONITOR),
"climb does not automatically degrade to repeated monitor calls");
260 Check(f.bus->TCAS >=
Bus::RA_Climb && f.bus->TCASTargetOrdinal == 1,
"live target displayed");
262 Check(f.bus->TCAS ==
Bus::Clear_Of_Conflict && f.bus->TCASTargetOrdinal == -1,
"GPWS clears actual advisory");
263}
264static void NuisanceAndTracking()
265{
266 { Fixture f; Traffic(f, 1, 3, 2000); Fly(f, 5);
267 Check(played.empty() && f.bus->TCASTargetOrdinal == -1, "vertically separated traffic is not a threat"); }
268 { Fixture f; auto& t = Traffic(f, 1, 3, 0); t.position.z = 3; Fly(f, 5);
269 Check(played.empty(), "large horizontal miss distance suppresses nuisance alerts"); }
270 { Fixture f; Traffic(f, 1, .3f, 0, 0, -.1f); Fly(f, 5);
271 Check(played.empty(), "receding traffic produces no alert"); }
272 { Fixture f; auto& t = Traffic(f, 1, 3, -100); Fly(f, 5);
273 Check(Heard(
SOUND_TCASCLIMB),
"traffic with transponder on is detected even with logo light off");
274 t.active = false; Fly(f, 2);
275 Check(f.bus->TCASTargetOrdinal == -1 && Heard(
SOUND_TCASCLEAR),
"lost target clears advisory and target");
276 played.clear(); t.active = true; t.vehicleId = 999; t.mslKm += 2; Fly(f, 3);
277 Check(played.empty(), "reused scanner slot acquires a new track without stale rate"); }
278 { Fixture f; auto& t = Traffic(f, 1, 3, -100, 600); Fly(f, 2.4f, 0, .6f);
279 Check(std::fabs(t.vsi-600*FPM) < 20*FPM && std::fabs(t.closurerate-.15f) < .003f,
280 "rates use actual 1.2-second sample interval"); }
281 { Fixture f; Traffic(f, 1, 3, -100); Fly(f, 4); played.clear(); f.Step(8);
283}
284static void LiveReduceAndMaintain()
285{
286 { Fixture f; Traffic(f, 1, 3.5f, 550); Fly(f, 3, 1500);
288 "converging climb reduces to level flight when that provides separation"); }
289 { Fixture f; Traffic(f, 1, 3.5f, -550); Fly(f, 3, -1500);
291 { Fixture f; Traffic(f, 1, 3, -80); Fly(f, 3, 1800);
293 "adequate existing climb gets maintain rate"); }
294 { Fixture f; Traffic(f, 1, 4, -100); Fly(f, 3); Fly(f, 12, 1500);
296}
297static void CrossingAndCamera()
298{
299 { Fixture f; Traffic(f, 1, 3, 200, -1200); Fly(f, 4);
301 "crossing climb selected when non-crossing descent cannot provide separation"); }
302 { Fixture f; Traffic(f, 1, 3, -200, 1200); Fly(f, 4);
306 "non-crossing sense preferred and camera orientation has no effect"); }
307}
308static void IncreaseReverseAndInhibits()
309{
310 { Fixture f; auto& t = Traffic(f, 1, 4, -30); Fly(f, 3, 1500);
311 Check(Heard(
SOUND_TCASMAINTAIN),
"maintain-strengthening scenario starts with adequate current climb");
312 t.velocity.y = 1200*FPM; Fly(f, 5, 1500);
314 { Fixture f; auto& t = Traffic(f, 1, 4, -100); Fly(f, 5);
316 t.velocity.y = 1200*FPM; Fly(f, 5, 1500);
318 { Fixture f; auto& t = Traffic(f, 1, 4, 100); Fly(f, 5);
319 t.velocity.y = -1200*FPM; Fly(f, 5, -1500);
321 { Fixture f; auto& t = Traffic(f, 1, 4, -100); Fly(f, 5);
322 t.velocity.y = 5000*FPM; Fly(f, 8, 1500);
323 Check(Heard(
SOUND_TCASDESCENDNOW),
"ineffective climb reverses after intruder crosses above"); }
324 { Fixture f; f.bus->RadioAltitudeKm = 1050*FT; Traffic(f, 1, 1.5f, 50); Fly(f, 5);
326 { Fixture f; f.bus->RadioAltitudeKm = 1300*FT; auto& t = Traffic(f, 1, 2, 100); Fly(f, 5);
327 t.velocity.y = -1200*FPM; Fly(f, 5, -1500);
329}
330static void MultiThreatAndSpeech()
331{
332 { Fixture f; auto& t = Traffic(f, 1, 4, -100); Fly(f, 4); played.clear();
333 t.velocity.z = .05f; Fly(f, 4);
335 "RA-to-TA downgrade clears once without re-announcing the same traffic"); }
336 { Fixture f; Traffic(f, 1, 3, 100); Traffic(f, 2, 3, -450); Fly(f, 4);
337 Check(f.bus->TCAS >=
Bus::RA_Climb,
"multiple intruders produce one shared advisory");
338 Check(f.bus->TCASTargetVerticalSpeedKms >= 0, "selection considers lower secondary threat before descending"); }
339 { Fixture f; auto& t = Traffic(f, 1, 3, -100); f.bus->vmuHold = 30; Fly(f, 3);
340 t.active = false; Fly(f, 2);
341 for (const auto& c : f.bus->commandStream)
342 Check(c.name != "TCASClimb" && c.name != "TCASCrossing", "superseded speech is purged while channel is busy"); }
343 { Fixture f; soundDuration = 10; Traffic(f, 1, 3, -100); Fly(f, 3);
345 Check(stopped.empty() && !Heard(
SOUND_TCASCLEAR),
"clear-of-conflict waits for the active TCAS phrase");
346 Fly(f, 10);
347 Check(stopped.empty() && Heard(
SOUND_TCASCLEAR),
"clear-of-conflict plays after the prior phrase finishes"); }
348}
349
350static void PreventiveSlowClosureAndIdentity()
351{
352 { Fixture f; Traffic(f, 1, 3, 550); Fly(f, 8);
354 "preventive advisory persists without repeated monitor calls"); }
355 { Fixture f; Traffic(f, 1, .2f, -30, 0, 0); Fly(f, 3);
356 Check(f.bus->TCAS >=
Bus::RA_Climb,
"fixed-distance threshold protects slow/zero closure encounters"); }
358 Check(Heard(
SOUND_TCASDESCEND),
"higher vehicle ID chooses down in an equal-cost encounter"); }
359 { Fixture f;
playerships[0].
vehicleId = 1;
auto& t = Traffic(f, 1, 3, 0); t.vehicleId = 2; Fly(f, 3);
360 Check(Heard(
SOUND_TCASCLIMB),
"lower vehicle ID chooses complementary up in equal-cost encounter"); }
361 { Fixture f; auto& t = Traffic(f, 1, 4, 100); Fly(f, 5);
362 t.velocity.y = -5000*FPM; Fly(f, 8, -1500);
363 Check(Heard(
SOUND_TCASCLIMBNOW),
"mirrored ineffective descent reverses to climb now"); }
364 { Fixture f; soundDuration = 10; auto& t = Traffic(f, 1, 3, 200, -1200); Fly(f, 3);
366 t.active = false; Fly(f, 2);
368 Fly(f, 10);
370}
371static void ContinuousPowerWarmup()
372{
373 Fixture f;
378 Check(!Heard(
SOUND_AURALUNITOKAY),
"interrupted power cannot accumulate VMU warm-up time");
379 f.Step(1);
381}
382
383static void FullVmuTestRegression()
384{
385 Fixture f;
387 f.Queue("VMUTest");
388 for (int i = 0; i < 200; ++i) f.Step(1);
389 Check(!f.bus->TCASTestActive && f.bus->TCASTestCallout == -1,
390 "full VMU audio test does not create an unrequested TCAS display test");
391 int tcasVoices = 0;
392 for (auto voice : played)
394 Check(tcasVoices == 15 && stopped.empty(), "full VMU test includes 15 TCAS voices on ground without truncation");
395}
396
397static void PendingTestControls()
398{
399 Fixture f;
400 f.bus->vmuHold = 5;
401 f.Queue("VMUTest"); f.Queue("TcasSystemTest"); f.Step(); f.Step();
402 Check(f.bus->TCASTestActive && f.bus->commandStream.empty(),
403 "new TCAS test supersedes an older unconsumed VMU test");
404 f.Queue("VMUTest"); f.Step();
405 Check(!f.bus->TCASTestActive, "new VMU test cancels TCAS while audio is busy");
406}
407
408static void BusySequence()
409{
411 Check(sequence.
Update(30,
true) == -1,
"busy VMU prevents queued test backlog");
412 Check(sequence.
Update(0,
false) == 0,
"first callout waits for VMU");
413 Check(sequence.
Update(0,
false) == -1,
"long frame does not burst remaining callouts");
415 Check(sequence.
Update(30,
false) == -1,
"canceled sequence stays canceled");
418 Check(sequence.
Update(3,
false) == i,
"silent test still steps through every display phase");
419 sequence.
Update(.1f,
false);
420 Check(sequence.
Active(),
"final OK display remains readable without an audio recording");
421 sequence.
Update(2,
false);
422 Check(!sequence.
Active(),
"silent test ends after the final display dwell");
423}
425{
426 InhibitsAndResets(); VoicesAndPreemption(); CompleteGroundTest(); CancelAndRestart();
427 DisplayPlaybackAndLifecycle();
428 LiveAdvisoryRegression(); NuisanceAndTracking(); LiveReduceAndMaintain(); CrossingAndCamera();
429 IncreaseReverseAndInhibits(); MultiThreatAndSpeech(); PreventiveSlowClosureAndIdentity(); ContinuousPowerWarmup(); FullVmuTestRegression(); PendingTestControls(); BusySequence();
430 std::cout << "PASS: live TCAS encounter selection, increase/reduce/crossing/reversal, tracking, phrase completion, and 15-callout TCAS/VMU regression suite\n";
431}
SCRIPT_CONTROL SCRIPT_STATE const void int
@ SOUND_TCASREDUCEDESCENT
@ SOUND_TCASINCREASECLIMB
@ SOUND_TCASINCREASEDESCENT
void Log(const char *msg, Level level=Info, int errorCode=0)
void Stop(SOUNDS soundEnum) const
std::vector< std::string > sound_path
float Play(SOUNDS soundEnum, float volume=1.0f)
Sound(GameClass *gameptr)
void FrameMove(float fElapsedTime) override
int Update(float seconds, bool busy)
void FrameMove(float fElapsedTime) override
s_network_objects playerships[MAX_SCAN]
Matrix * MatrixRotationX(Matrix *out, float value)
Matrix * MatrixIdentity(Matrix *out)
int Find(const char *command)
constexpr Callout Callouts[]
@ Mode7_Windshear_Warning
RiseMath::Vector3 position
RiseMath::Vector3 velocity