Skip to content

Commit

Permalink
Fix TCAS aircraft count
Browse files Browse the repository at this point in the history
  • Loading branch information
fpw committed Jan 26, 2021
1 parent a5e80fd commit 56ece3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.9)
project(AviTab VERSION 0.4.5 DESCRIPTION "AviTab X-Plane plugin")
project(AviTab VERSION 0.4.6 DESCRIPTION "AviTab X-Plane plugin")

if (NOT "$ENV{NAVIGRAPH_SECRET}" STREQUAL "")
set(NAVIGRAPH_SECRET "$ENV{NAVIGRAPH_SECRET}" CACHE INTERNAL "Copied from environment variable")
Expand Down
20 changes: 14 additions & 6 deletions src/environment/xplane/XPlaneEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,17 @@ float XPlaneEnvironment::onFlightLoop(float elapsedSinceLastCall, float elapseSi

updatePlaneCount();
for (AircraftID i = 0; i <= otherAircraftCount; ++i) {
Location loc;
loc.latitude = dataCache.getLocationData(i, 0).doubleValue;
loc.longitude = dataCache.getLocationData(i, 1).doubleValue;
loc.elevation = dataCache.getLocationData(i, 2).doubleValue;
loc.heading = dataCache.getLocationData(i, 3).floatValue;
activeAircraftLocations.push_back(loc);
try {
Location loc;
loc.latitude = dataCache.getLocationData(i, 0).doubleValue;
loc.longitude = dataCache.getLocationData(i, 1).doubleValue;
loc.elevation = dataCache.getLocationData(i, 2).doubleValue;
loc.heading = dataCache.getLocationData(i, 3).floatValue;
activeAircraftLocations.push_back(loc);
} catch (const std::exception &e) {
// silently ignore to avoid flooding the log
// can fail with TCAS override, more than 19 AI aircraft
}
}

{
Expand Down Expand Up @@ -389,6 +394,9 @@ void XPlaneEnvironment::updatePlaneCount() {
XPLMCountAircraft(&tmp1, &active, &tmp2);
if (active > 0) {
otherAircraftCount = active - 1;
if (otherAircraftCount > MAX_AI_AIRCRAFT) {
otherAircraftCount = MAX_AI_AIRCRAFT;
}
} else {
otherAircraftCount = 0;
}
Expand Down

2 comments on commit 56ece3a

@mjh65
Copy link
Collaborator

@mjh65 mjh65 commented on 56ece3a Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Folke,
Thanks for fixing this bug in my code! I hadn't realised that XPLMCountAircraft() could return more than 20, and don't have a TCAS generating plugin that would test this.
It occurs to me that the try/catch update should not be required as you are already making sure that otherAircraftCount is limited in the updatePlaneCount() function.
Mike

@fpw
Copy link
Owner Author

@fpw fpw commented on 56ece3a Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mjh65,

no problem, I wasn't aware of this change in XPLMCountAircraft either. I think this is a rather recent change, I found some information here: https://developer.x-plane.com/article/overriding-tcas-and-providing-traffic-information/

But I didn't have time to fully understand that article yet, so for now it's enough to have this fixed.

You're right that the catch handler is currently superfluous. The idea was basically: If any part of the flight loop throws an unhandled exception, there should at least be some central catch handler so we don't crash X-Plane. That way, it's prepared for exceptions from future changes to the flight loop.

-- Folke

Please sign in to comment.