swift
Loading...
Searching...
No Matches
simconnectobject.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "simconnectobject.h"
5
7
9#include "core/simulator.h"
11#include "misc/stringutils.h"
12
13using namespace swift::config;
14using namespace swift::misc;
15using namespace swift::misc::aviation;
16using namespace swift::misc::simulation;
17using namespace swift::core;
18
19namespace swift::simplugin::fsxcommon
20{
22
27
29 ISimulationEnvironmentProvider *simEnvProvider,
30 IInterpolationSetupProvider *setupProvider,
31 IRemoteAircraftProvider *remoteAircraftProvider, CInterpolationLogger *logger)
32 : m_aircraft(aircraft), m_requestId(requestId), m_validRequestId(true),
33 m_interpolator(QSharedPointer<CInterpolatorMulti>::create(aircraft.getCallsign(), simEnvProvider,
34 setupProvider, remoteAircraftProvider, logger))
35 {
37 m_type = aircraft.isTerrainProbe() ? TerrainProbe : AircraftNonAtc;
38 m_interpolator->initCorrespondingModel(aircraft.getModel());
39 m_callsignByteArray = aircraft.getCallsignAsString().toLatin1();
40 }
41
43 {
44 m_aircraft = aircraft;
45 m_callsignByteArray = aircraft.getCallsignAsString().toLatin1();
46 m_type = aircraft.isTerrainProbe() ? TerrainProbe : AircraftNonAtc;
47 }
48
50 {
51 if (modelString.isEmpty())
52 {
53 return;
54 }
55 m_aircraft.setModelString(modelString);
56 }
57
59 {
60 if (cg.isNull())
61 {
62 return;
63 }
64 m_aircraft.setCG(cg);
65 }
66
68 {
69 m_requestId = id;
70 m_validRequestId = true;
71 const SimObjectType type = requestIdToType(id);
72 this->setType(type);
73 }
74
76 {
78 {
79 const SimObjectType type = requestIdToType(m_requestId);
80 const bool same = CSimConnectObject::isSameTypeGroup(type, this->getType());
81 Q_ASSERT_X(same, Q_FUNC_INFO, "Type mismatch");
82 }
83
84 DWORD os = 0;
85 switch (this->getType())
86 {
87 case TerrainProbe: os = static_cast<DWORD>(CSimulatorFsxCommon::offsetSimObjTerrainProbe(offset)); break;
88 case AircraftNonAtc:
89 case AircraftSimulatedObject:
90 default: os = static_cast<DWORD>(CSimulatorFsxCommon::offsetSimObjAircraft(offset)); break;
91 }
92 return os + m_requestId;
93 }
94
96 {
97 m_objectId = id;
98 m_validObjectId = true;
99 }
100
101 bool CSimConnectObject::isPendingAdded() const { return !this->hasValidRequestAndObjectId() || !m_confirmedAdded; }
102
103 bool CSimConnectObject::isOutdatedPendingAdded(qint64 thresholdMs, qint64 currentMsSinceEpoch) const
104 {
105 if (!this->isPendingAdded())
106 {
107 return false;
108 }
109 if (currentMsSinceEpoch < 0)
110 {
111 currentMsSinceEpoch = QDateTime::currentMSecsSinceEpoch();
112 }
113 if (m_tsCreated < 0)
114 {
115 return true;
116 } // no valid timestamp
117 const qint64 delta = currentMsSinceEpoch - m_tsCreated;
118 return delta > thresholdMs;
119 }
120
122 {
123 Q_ASSERT_X(!m_confirmedAdded || this->hasValidRequestAndObjectId(), Q_FUNC_INFO, "confirmed but invalid ids");
124 return m_confirmedAdded;
125 }
126
128 {
129 m_confirmedAdded = confirm;
130 m_removedWhileAdding = false;
131 m_addedWhileRemoving = false;
132 m_aircraft.setRendered(true);
133 }
134
135 void CSimConnectObject::setAddedWhileRemoving(bool addedWileRemoved) { m_addedWhileRemoving = addedWileRemoved; }
136
137 void CSimConnectObject::setRemovedWhileAdding(bool removedWhileAdding)
138 {
139 m_removedWhileAdding = removedWhileAdding;
140 }
141
143 {
144 return !this->isPending() && !m_addedWhileRemoving && !m_removedWhileAdding;
145 }
146
148 {
149 m_pendingRemoved = pending;
150 m_removedWhileAdding = false;
151 m_addedWhileRemoving = false;
152 m_aircraft.setRendered(false);
153 }
154
156 {
157 m_cameraPosition.x = 0;
158 m_cameraPosition.y = 0;
159 m_cameraPosition.z = 0;
160 m_cameraRotation.Pitch = 0;
161 m_cameraRotation.Bank = 0;
162 m_cameraRotation.Heading = 0;
163 }
164
166 {
167 m_pendingRemoved = false;
168 m_confirmedAdded = false;
169 m_removedWhileAdding = false;
170 m_addedWhileRemoving = false;
171 m_camera = false;
172 m_currentLightsInSim = CAircraftLights();
173 m_lightsAsSent = CAircraftLights();
174 m_requestId = 0;
175 m_objectId = 0;
176 m_addingExceptions = 0;
177 m_validRequestId = false;
178 m_validObjectId = false;
179 m_tsCreated = -1;
180 this->resetCameraPositions();
181 }
182
184 {
185 const CSimConnectObject old(*this);
186 this->resetState();
187 this->copyAddingFailureCounters(old);
188 }
189
191 {
192 return this->hasValidRequestId() && this->hasValidObjectId();
193 }
194
196 {
197 m_addingExceptions = otherObject.m_addingExceptions;
198 m_addingDirectlyRemoved = otherObject.m_addingDirectlyRemoved;
199 }
200
202 {
203 Q_ASSERT(m_interpolator);
204 return m_interpolator->getInterpolatorInfo(mode);
205 }
206
208 {
209 Q_ASSERT(m_interpolator);
210 m_interpolator->attachLogger(logger);
211 }
212
215 uint32_t aircraftNumber) const
216 {
217 if (!m_interpolator)
218 {
219 return {};
220 }
221 return m_interpolator->getInterpolation(currentTimeSinceEpoch, setup, aircraftNumber);
222 }
223
224 const CAircraftSituation &
226 {
227 if (!m_interpolator)
228 {
230 }
231 return m_interpolator->getLastInterpolatedSituation(mode);
232 }
233
234 const CStatusMessageList &
236 {
237 static const CStatusMessageList empty;
238 if (!m_interpolator)
239 {
240 return empty;
241 }
242 return m_interpolator->getInterpolationMessages(mode);
243 }
244
246 {
247 static const QString s(
248 "CS: '%1' obj: %2 req: %3 conf.added: %4 pend.rem.: %5 rwa: %6 awr: %7 aEx: %8 aRem: %9");
249 return s.arg(this->getCallsign().asString())
250 .arg(m_objectId)
251 .arg(m_requestId)
252 .arg(boolToYesNo(m_confirmedAdded), boolToYesNo(m_pendingRemoved), boolToYesNo(m_removedWhileAdding),
253 boolToYesNo(m_addedWhileRemoving))
254 .arg(m_addingExceptions)
255 .arg(m_addingDirectlyRemoved);
256 }
257
259 {
261 {
262 return TerrainProbe;
263 }
265 {
266 return AircraftNonAtc;
267 }
268 Q_ASSERT_X(false, Q_FUNC_INFO, "Wrong range");
269 return AircraftNonAtc;
270 }
271
273 {
274 static const QString a1("aircraft (non ATC)");
275 static const QString a2("aircraft (sim.object)");
276 static const QString p("probe");
277 static const QString u("unknown");
278 switch (type)
279 {
280 case AircraftNonAtc: return a1;
281 case AircraftSimulatedObject: return a2;
282 case TerrainProbe: return p;
283 default: break;
284 }
285 return u;
286 }
287
289 {
290 if (t1 == t2)
291 {
292 return true;
293 }
294 return isAircraft(t1) && isAircraft(t2);
295 }
296
298 {
299 return CSimConnectObject::AircraftNonAtc == type || CSimConnectObject::AircraftSimulatedObject == type;
300 }
301
302 bool CSimConnectObjects::insert(const CSimConnectObject &simObject, bool updateTimestamp)
303 {
304 if (!simObject.hasCallsign())
305 {
306 return false;
307 }
308 if (updateTimestamp)
309 {
310 CSimConnectObject simObj(simObject);
311 simObj.resetTimestampToNow();
312 (*this)[simObj.getCallsign()] = simObj;
313 }
314 else
315 {
316 (*this)[simObject.getCallsign()] = simObject;
317 }
318 return true;
319 }
320
322 {
323 // First check, if this request id belongs to us
324 auto it = std::find_if(this->begin(), this->end(),
325 [requestId](const CSimConnectObject &obj) { return obj.getRequestId() == requestId; });
326 if (it == this->end())
327 {
328 return false;
329 }
330
331 // belongs to us
332 it->setObjectId(objectId);
333 return true;
334 }
335
337 {
338 return this->getSimObjectForObjectId(objectId).getCallsign();
339 }
340
342 {
343 if (this->isEmpty())
344 {
345 return CCallsignSet();
346 }
347 if (!withoutProbes)
348 {
349 return CCallsignSet(this->keys());
350 }
351 CCallsignSet callsigns;
352 for (const CSimConnectObject &simObject : *this)
353 {
354 if (simObject.isAircraft())
355 {
356 callsigns.insert(simObject.getCallsign().asString());
357 }
358 }
359 return callsigns;
360 }
361
362 QStringList CSimConnectObjects::getAllCallsignStrings(bool sorted, bool withoutProbes) const
363 {
364 return this->getAllCallsigns(withoutProbes).getCallsignStrings(sorted);
365 }
366
368 {
369 return this->getAllCallsignStrings(sorted).join(separator);
370 }
371
373 {
374 for (const CSimConnectObject &simObject : *this)
375 {
376 if (simObject.getObjectId() == objectId)
377 {
378 return simObject;
379 }
380 }
381 return CSimConnectObject();
382 }
383
385 {
386 if (this->isEmpty())
387 {
388 return CSimConnectObject();
389 }
390 CSimConnectObject oldestSimObj = *this->begin();
391 for (const CSimConnectObject &simObj : *this)
392 {
393 if (!simObj.hasCreatedTimestamp())
394 {
395 continue;
396 }
397 if (!oldestSimObj.hasCreatedTimestamp() ||
398 oldestSimObj.getCreatedTimestamp() > simObj.getCreatedTimestamp())
399 {
400 oldestSimObj = simObj;
401 }
402 }
403 return oldestSimObj;
404 }
405
407 {
408 for (const CSimConnectObject &simObject : *this)
409 {
410 if (simObject.getRequestId() == requestId)
411 {
412 return simObject;
413 }
414 }
415 return CSimConnectObject();
416 }
417
419 {
420 if (otherSimObj.hasValidObjectId())
421 {
422 CSimConnectObject obj = this->getSimObjectForObjectId(otherSimObj.getObjectId());
423 if (!obj.isInvalid())
424 {
425 return obj;
426 }
427 }
428 if (!otherSimObj.hasValidRequestId())
429 {
430 return CSimConnectObject();
431 }
432 return this->getSimObjectForRequestId(otherSimObj.getRequestId());
433 }
434
436 {
437 const CSimConnectObject simObject(this->getSimObjectForObjectId(objectId));
438 return simObject.hasValidRequestAndObjectId() && objectId == simObject.getObjectId();
439 }
440
442 {
443 const CSimConnectObject simObject(this->getSimObjectForObjectId(objectId));
444 const int c = this->remove(simObject.getCallsign());
445 return c > 0;
446 }
447
449 {
450 const int c = this->remove(otherSimObj.getCallsign());
451 return c > 0;
452 }
453
455 {
456 const QList<CSimConnectObject> probes = this->getProbes();
457 int c = 0;
458 for (const CSimConnectObject &probe : probes)
459 {
460 this->remove(probe.getCallsign());
461 c++;
462 }
463 return c;
464 }
465
467 {
468 for (const CSimConnectObject &simObject : *this)
469 {
470 if (simObject.isPendingAdded())
471 {
472 return true;
473 }
474 }
475 return false;
476 }
477
479 {
480 for (const CSimConnectObject &simObject : *this)
481 {
482 if (simObject.isPendingRemoved())
483 {
484 return true;
485 }
486 }
487 return false;
488 }
489
491 {
492 int c = 0;
493 for (const CSimConnectObject &simObject : *this)
494 {
495 if (simObject.isPendingAdded())
496 {
497 c++;
498 }
499 }
500 return c;
501 }
502
504 {
505 int c = 0;
506 for (const CSimConnectObject &simObject : *this)
507 {
508 if (simObject.isPendingRemoved())
509 {
510 c++;
511 }
512 }
513 return c;
514 }
515
517 {
518 int c = 0;
519 for (const CSimConnectObject &simObject : std::as_const(*this))
520 {
521 if (simObject.isConfirmedAdded())
522 {
523 c++;
524 }
525 }
526 return c;
527 }
528
530 {
531 CCallsignSet callsigns;
532 for (const CSimConnectObject &simObject : *this)
533 {
534 if (simObject.isPendingAdded())
535 {
536 callsigns.push_back(simObject.getCallsign());
537 }
538 }
539 return callsigns;
540 }
541
543 {
544 CCallsignSet callsigns;
545 for (const CSimConnectObject &simObject : *this)
546 {
547 if (simObject.isPendingRemoved())
548 {
549 callsigns.push_back(simObject.getCallsign());
550 }
551 }
552 return callsigns;
553 }
554
556 {
558 for (const CSimConnectObject &simObject : *this)
559 {
560 if (simObject.getType() == type)
561 {
562 objs.push_back(simObject);
563 }
564 }
565 return objs;
566 }
567
569 {
570 QList<CSimConnectObject> l = this->getByType(CSimConnectObject::AircraftNonAtc);
571 l.append(this->getByType(CSimConnectObject::AircraftSimulatedObject));
572 return l;
573 }
574
576 {
577 for (const CSimConnectObject &simObject : *this)
578 {
579 if (simObject.getType() == CSimConnectObject::TerrainProbe && !simObject.isPending())
580 {
581 return simObject;
582 }
583 }
584 return CSimConnectObject();
585 }
586
588 {
589 CSimConnectObject oldestProbe;
590 for (const CSimConnectObject &simObject : *this)
591 {
592 if (simObject.getType() == CSimConnectObject::TerrainProbe && !simObject.isPending())
593 {
594 if (!oldestProbe.hasCreatedTimestamp() ||
595 oldestProbe.getCreatedTimestamp() > simObject.getCreatedTimestamp())
596 {
597 oldestProbe = simObject;
598 }
599 }
600 }
601 return oldestProbe;
602 }
603
605 {
606 for (const CSimConnectObject &simObject : *this)
607 {
608 if (simObject.getType() == type)
609 {
610 return true;
611 }
612 }
613 return false;
614 }
615
617 {
618 return this->containsType(CSimConnectObject::AircraftNonAtc) ||
619 this->containsType(CSimConnectObject::AircraftSimulatedObject);
620 }
621
623 {
624 int c = 0;
625 for (const CCallsign &cs : callsigns)
626 {
627 c += this->remove(cs);
628 }
629 return c;
630 }
631
633 {
634 CCallsignSet removeCallsignsSet;
635 CSimConnectObjects removedObjects;
636
637 const qint64 ts = QDateTime::currentMSecsSinceEpoch();
638 for (const CSimConnectObject &simObject : std::as_const(*this))
639 {
640 // verification takes at least a second, so we need some time before outdating
641 if (type != CSimConnectObject::AllTypes && simObject.getType() != type)
642 {
643 continue;
644 }
645 if (!simObject.isOutdatedPendingAdded(5000, ts))
646 {
647 continue;
648 }
649 removedObjects.insert(simObject);
650 removeCallsignsSet.insert(simObject.getCallsign());
651 }
652 if (!removeCallsignsSet.isEmpty())
653 {
654 this->removeCallsigns(removeCallsignsSet);
655 }
656 return removedObjects;
657 }
658} // namespace swift::simplugin::fsxcommon
static bool isLocalDeveloperDebugBuild()
Local build for developers.
iterator insert(const_iterator hint, const T &value)
For compatibility with std::inserter.
Definition collection.h:205
bool isEmpty() const
Synonym for empty.
Definition collection.h:197
iterator push_back(const T &value)
Synonym for insert.
Definition collection.h:244
Status messages, e.g. from Core -> GUI.
Value object encapsulating information about aircraft's lights.
Value object encapsulating information of an aircraft's situation.
static const CAircraftSituation & null()
Null situation.
Value object encapsulating information of a callsign.
Definition callsign.h:30
const QString & asString() const
Get callsign (normalized).
Definition callsign.h:96
Value object for a set of callsigns.
Definition callsignset.h:26
QStringList getCallsignStrings(bool sorted=false) const
The callsign strings.
Physical unit length (length).
Definition length.h:18
Value object for interpolator and rendering per callsign.
Record internal state of interpolator for debugging.
Multiplexed interpolator which allows switching between modes at runtime.
Comprehensive information of an aircraft.
const simulation::CAircraftModel & getModel() const
Get model (model used for mapping).
QString getCallsignAsString() const
Get callsign.
Direct in memory access to interpolation setup, normally implemented by simulator.
Direct thread safe in memory access to remote aircraft.
SimObjectRequest
SimObject requests used for AI aircraft and probes.
Class representing a SimConnect object.
bool isPendingAdded() const
Object is requested in simulator, not yet confirmed added.
bool isConfirmedAdded() const
Adding is confirmed.
bool hasValidRequestId() const
Valid request id?
void setConfirmedAdded(bool confirm)
Marked as confirmed, means the simulator has "confirmed" the objectId as added and not instantly remo...
void setAddedWhileRemoving(bool addedWileRemoved)
Special states.
const swift::misc::aviation::CCallsign & getCallsign() const
Get callsign.
const swift::misc::aviation::CAircraftSituation & getLastInterpolatedSituation(swift::misc::simulation::CInterpolationAndRenderingSetupBase::InterpolatorMode mode) const
Latest interpolation result.
DWORD getObjectId() const
Get SimConnect object id.
static bool isSameTypeGroup(SimObjectType t1, SimObjectType t2)
Same type.
QString toQString() const
SimObject as string.
bool hasValidRequestAndObjectId() const
Was the object really added to simulator.
swift::misc::simulation::CInterpolationResult getInterpolation(qint64 currentTimeSinceEpoch, const swift::misc::simulation::CInterpolationAndRenderingSetupPerCallsign &setup, uint32_t aircraftNumber) const
Get interpolated situation.
bool isPending() const
Pending added or removed?
void setRequestId(DWORD id)
Set Simconnect request id.
static SimObjectType requestIdToType(DWORD requestId)
Type of id.
static const QString & typeToString(SimObjectType type)
Type to string.
void resetToAddAgain()
Reset so it can be added again.
DWORD getRequestId() const
Get SimConnect request id.
void setAircraftCG(const swift::misc::physical_quantities::CLength &cg)
Set CG.
void attachInterpolatorLogger(swift::misc::simulation::CInterpolationLogger *logger) const
Attach an observer to read the interpolator's state for debugging.
void resetState()
Reset the state (like it was a new onject) without affecting interpolator and aircraft.
bool isOutdatedPendingAdded(qint64 thresholdMs=5000, qint64 currentMsSinceEpoch=-1) const
Still pending.
qint64 getCreatedTimestamp() const
Created timestamp.
void setRemovedWhileAdding(bool removedWhileAdding)
Special states.
void setAircraftModelString(const QString &modelString)
Set model string.
void setType(SimObjectType type)
Set the type.
void setAircraft(const swift::misc::simulation::CSimulatedAircraft &aircraft)
Set the aircraft.
bool isPendingRemoved() const
Removing is pending.
void copyAddingFailureCounters(const CSimConnectObject &otherObject)
Copy the counters from another object.
bool hasCreatedTimestamp() const
Created timestamp?
QString getInterpolatorInfo(swift::misc::simulation::CInterpolationAndRenderingSetupBase::InterpolatorMode mode) const
Get an interpolator info string (for debug info).
void setPendingRemoved(bool pending)
Marked as pending for removal.
SimObjectType getType() const
Object type.
void setObjectId(DWORD id)
Set Simconnect object id.
const swift::misc::CStatusMessageList & getInterpolationMessages(swift::misc::simulation::CInterpolationAndRenderingSetupBase::InterpolatorMode mode) const
Interpolation messages.
bool isReadyToSend() const
Object which can be used for sending, not pending and valid ids.
Simulator objects (aka AI aircraft).
bool containsPendingRemoved() const
Pending removed condition.
CSimConnectObject getSimObjectForRequestId(DWORD requestId) const
Get object per request id.
bool removeByOtherSimObject(const CSimConnectObject &otherSimObj)
Remove by object id or request id.
int countPendingAdded() const
Number of pending added.
int countPendingRemoved() const
Number of pending removed.
swift::misc::aviation::CCallsignSet getAllCallsigns(bool withoutProbes=true) const
Get all callsigns.
CSimConnectObject getSimObjectForObjectId(DWORD objectId) const
Get object per object id.
bool removeByObjectId(DWORD objectId)
Remove by id.
bool insert(const CSimConnectObject &simObject, bool updateTimestamp=false)
Insert.
swift::misc::aviation::CCallsignSet getPendingRemovedCallsigns() const
Callsigns of pending removed callsigns.
CSimConnectObject getSimObjectForOtherSimObject(const CSimConnectObject &otherSimObj) const
Get by request or object id, just as possible.
bool setSimConnectObjectIdForRequestId(DWORD requestId, DWORD objectId)
Set ID of a SimConnect object, so far we only have an request id in the object.
CSimConnectObject getOldestNotPendingProbe() const
Get a non pending probe.
CSimConnectObject getNotPendingProbe() const
Get a non pending probe.
swift::misc::aviation::CCallsignSet getPendingAddedCallsigns() const
Callsigns of pending added callsigns.
QList< CSimConnectObject > getByType(CSimConnectObject::SimObjectType type) const
Get by type.
CSimConnectObject getOldestObject() const
Get the oldest object.
bool containsType(CSimConnectObject::SimObjectType type) const
Contains object of type.
bool isKnownSimObjectId(DWORD objectId) const
Is the object id one of our AI objects?
QString getAllCallsignStringsAsString(bool sorted=false, const QString &separator=", ") const
Get all callsign strings as string.
QList< CSimConnectObject > getProbes() const
All probes.
bool containsPendingAdded() const
Pending add condition.
int removeCallsigns(const swift::misc::aviation::CCallsignSet &callsigns)
Remove callsigns.
CSimConnectObjects removeOutdatedPendingAdded(CSimConnectObject::SimObjectType type)
Remove all pending added objects.
QList< CSimConnectObject > getAircraft() const
All aircraft.
swift::misc::aviation::CCallsign getCallsignForObjectId(DWORD objectId) const
Find which callsign belongs to the object id.
QStringList getAllCallsignStrings(bool sorted=false, bool withoutProbes=true) const
Get all callsign strings.
static DWORD offsetSimObjAircraft(CSimConnectDefinitions::SimObjectRequest req)
Offsets.
static bool isRequestForSimObjTerrainProbe(DWORD requestId)
Request for probe (elevation)?
static bool isRequestForSimObjAircraft(DWORD requestId)
Request for sim data (request in range of sim data)?
static DWORD offsetSimObjTerrainProbe(CSimConnectDefinitions::SimObjectRequest req)
Offsets.
Backend services of the swift project, like dealing with the network or the simulators.
Definition actionbind.cpp:7
Free functions in swift::misc.
SWIFT_MISC_EXPORT const QString & boolToYesNo(bool v)
Bool to yes/no.
qint64 currentMSecsSinceEpoch()
QHash< swift::misc::aviation::CCallsign, CSimConnectObject >::iterator begin()
QHash< swift::misc::aviation::CCallsign, CSimConnectObject >::iterator end()
QList< swift::misc::aviation::CCallsign > keys() const const
bool remove(const swift::misc::aviation::CCallsign &key)
void append(QList< T > &&value)
void push_back(QList< T >::parameter_type value)
QString arg(Args &&... args) const const
bool isEmpty() const const
QByteArray toLatin1() const const
QString join(QChar separator) const const
unsigned long DWORD
Fake Windows DWORD.