Skip to content

Commit

Permalink
Merge pull request #86 from mjh65/fix_stationary_planes2
Browse files Browse the repository at this point in the history
Fix stationary aircraft bug
  • Loading branch information
fpw authored Jan 25, 2021
2 parents 235ba25 + 85a9dc5 commit 1c578e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
37 changes: 16 additions & 21 deletions src/environment/xplane/XPlaneEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ XPlaneEnvironment::XPlaneEnvironment() {
xplaneRootPath = getXPlanePath();
xplaneData = std::make_shared<xdata::XData>(xplaneRootPath);

for (size_t i = 0; i < MAX_AI_AIRCRAFT + 1; ++i) {
prevLocations.push_back(nullLocation);
noMovementCount.push_back(NO_MOVEMENT_THRESHOLD);
}
updatePlaneCount();

panelEnabled = std::make_shared<int>(0);
panelPowered = std::make_shared<int>(0);
Expand Down Expand Up @@ -272,27 +269,14 @@ void XPlaneEnvironment::runInEnvironment(EnvironmentCallback cb) {
float XPlaneEnvironment::onFlightLoop(float elapsedSinceLastCall, float elapseSinceLastLoop, int count) {
std::vector<Location> activeAircraftLocations;

for (AircraftID i = 0; i <= MAX_AI_AIRCRAFT; ++i) {
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;
// only add user aircraft or ones that appear to be active
noMovementCount[i] += 1;
if ((i == 0) ||
((loc.elevation > 0.0) &&
((loc.latitude != prevLocations[i].latitude) ||
(loc.longitude != prevLocations[i].longitude) ||
(loc.heading != prevLocations[i].heading)))) {
noMovementCount[i] = 0;
}
if (noMovementCount[i] < NO_MOVEMENT_THRESHOLD) {
activeAircraftLocations.push_back(loc);
} else {
noMovementCount[i] = NO_MOVEMENT_THRESHOLD;
}
prevLocations[i] = loc;
activeAircraftLocations.push_back(loc);
}

{
Expand All @@ -308,7 +292,7 @@ float XPlaneEnvironment::onFlightLoop(float elapsedSinceLastCall, float elapseSi

AircraftID XPlaneEnvironment::getActiveAircraftCount() {
std::lock_guard<std::mutex> lock(stateMutex);
return aircraftLocations.size();
return (otherAircraftCount + 1);
}

Location XPlaneEnvironment::getAircraftLocation(AircraftID id) {
Expand Down Expand Up @@ -399,4 +383,15 @@ void XPlaneEnvironment::onAircraftReload() {
reloadAircraftPath();
}

void XPlaneEnvironment::updatePlaneCount() {
int tmp1, active;
XPLMPluginID tmp2;
XPLMCountAircraft(&tmp1, &active, &tmp2);
if (active > 0) {
otherAircraftCount = active - 1;
} else {
otherAircraftCount = 0;
}
}

} /* namespace avitab */
6 changes: 3 additions & 3 deletions src/environment/xplane/XPlaneEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ class XPlaneEnvironment: public Environment {
DataCache dataCache;
std::string pluginPath, xplanePrefsDir, xplaneRootPath;
std::shared_ptr<xdata::XData> xplaneData;
std::vector<Location> prevLocations;
std::vector<int> noMovementCount;
const int NO_MOVEMENT_THRESHOLD = 100;
std::vector<Location> aircraftLocations;
Location nullLocation { 0, 0, 0, 0 };
std::atomic<float> lastDrawTime{};
Expand All @@ -101,6 +98,9 @@ class XPlaneEnvironment: public Environment {
static int handleCommand(XPLMCommandRef cmd, XPLMCommandPhase phase, void *ref);
EnvData getData(const std::string &dataRef);
void reloadAircraftPath();

unsigned int otherAircraftCount;
void updatePlaneCount();
};

} /* namespace avitab */
Expand Down

0 comments on commit 1c578e5

Please sign in to comment.