XPMP2
X-Plane multiplayer library 2 - using instancing
XPMP2.h
Go to the documentation of this file.
1 
21 #ifndef _XPMP2_h_
22 #define _XPMP2_h_
23 
24 #if IBM
25 // In MINGW winsock2 must be included before windows.h, which is included by the mingw.*.h headers
26 #include <winsock2.h>
27 #include <ws2ipdef.h> // required for sockaddr_in6 (?)
28 #include <iphlpapi.h> // for GetAdaptersAddresses
29 #include <ws2tcpip.h>
30 // On Windows, 'max' and 'min' are defined macros in conflict with C++ library. Let's undefine them!
31 #include <direct.h>
32 #undef max
33 #undef min
34 #endif
35 
36 // XPlaneMP 2 - Public Header Files
37 #include "XPMPMultiplayer.h"
38 #include "XPMPAircraft.h"
39 #include "XPCAircraft.h"
40 #include "XPMPPlaneRenderer.h"
41 #include "XPMPRemote.h"
42 
43 // Standard C
44 #include <sys/stat.h>
45 #include <cmath>
46 #include <cstdarg>
47 #include <cassert>
48 
49 // Standard C++
50 #include <string>
51 #include <list>
52 #include <map>
53 #include <set>
54 #include <array>
55 #include <vector>
56 #include <queue>
57 #include <valarray>
58 #include <algorithm>
59 #include <numeric>
60 #include <fstream>
61 #include <future>
62 #include <thread>
63 #include <shared_mutex>
64 #include <regex>
65 #include <bitset>
66 
67 // FMOD Sound, must be included before XPLMSound.h
68 #if INCLUDE_FMOD_SOUND + 0 >= 1
69 #include "fmod_errors.h" // This one includes everything for the CORE API
70 #include "fmod_studio.h"
71 #endif
72 
73 // X-Plane SDK
74 #include "XPLMUtilities.h"
75 #include "XPLMScenery.h"
76 #include "XPLMProcessing.h"
77 #include "XPLMGraphics.h"
78 #include "XPLMDataAccess.h"
79 #include "XPLMPlugin.h"
80 #include "XPLMDisplay.h"
81 #include "XPLMCamera.h"
82 #include "XPLMPlanes.h"
83 #include "XPLMMap.h"
84 #include "XPLMSound.h"
85 
86 // XPlaneMP 2 - Internal Header Files
87 #include "Utilities.h"
88 #include "RelatedDoc8643.h"
89 #include "CSLModels.h"
90 #include "Aircraft.h"
91 #include "Contrail.h"
92 #include "2D.h"
93 #include "AIMultiplayer.h"
94 #include "Map.h"
95 #include "Network.h"
96 #include "Remote.h"
97 #include "Sound.h"
98 #if INCLUDE_FMOD_SOUND + 0 >= 1
99 #include "SoundFMOD.h"
100 #endif
101 
102 //
103 // MARK: Global Configurations and variables
104 //
105 
106 #define UNKNOWN_PLUGIN_NAME "(unknown)"
107 
108 namespace XPMP2 {
109 
111 enum ThreeWaySwitchTy : int {
115 };
116 
120  void* refcon = nullptr;
121 
122  XPMPPlaneNotifierTy (XPMPPlaneNotifier_f _func = nullptr, void* _refcon = nullptr) :
123  func(_func), refcon(_refcon) {}
124 
126  { return func == o.func && refcon == o.refcon; }
127 };
128 
129 typedef std::list<XPMPPlaneNotifierTy> listXPMPPlaneNotifierTy;
130 
132 void XPMPSendNotification (const Aircraft& plane, XPMPPlaneNotification _notification);
133 
135 struct GlobVars {
136 public:
137  // Settings:
141  bool bLogMdlMatch= false;
143  bool bClampAll = false;
145  bool bHandleDupId= false;
146 
148  bool bObjReplDataRefs = false;
150  bool bObjReplTextures = true;
152  std::string pathObj8DataRefs;
155 
160 
162  int (*prefsFuncInt)(const char *, const char *, int) = XPMP2::PrefsFuncIntDefault;
165 
167  std::string pathDoc8643;
171  std::array<std::string,REL_TXT_NUM> pathRelated = {};
173  std::array<mapRelatedTy,REL_TXT_NUM> mapRelated = {};
174 
180  std::string defaultICAO = "A320";
182  std::string carIcaoType = "ZZZC";
184  std::string resourceDir;
185 
189  bool bDrawLabels = true;
193  float maxLabelDist = 5556.0f;
197  float labelFontScaling = 1.0f;
198 
203 
205  bool bMapEnabled = true;
207  bool bMapLabels = true;
211  std::string pathMapIcons;
212 
215  std::string remoteMCGroup = "239.255.1.1"; // for IPv6 try "FF02::1"
217  int remotePort = 49788;
219  std::string remoteSendIntf;
221  bool remoteAutoDiscovery() const { return remoteSendIntf.empty(); }
223  int remoteTTL = 8;
225  size_t remoteBufSize = 8192;
227  int remoteTxfFrequ = 5;
238 
239  // Contrails
240  int contrailAltMin_ft = 25000;
241  int contrailAltMax_ft = 45000;
242  int contrailLifeTime = 25;
243  bool contrailMulti = false;
244 
246  bool bSoundOnStartup = true;
250  bool bSoundMuteOnPause = true;
252  bool bSoundAvail = false;
254  float sndMasterVol = 1.0f;
256  bool bSoundAutoMuted = false;
257 
259  int verXPlane = -1;
261  int verXPLM = -1;
265  bool bXPNetworkedSetup = false;
267  XPLMPluginID pluginId = 0;
269  std::thread::id xpThread;
270 
272  XPLMCameraPosition_t posCamera = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
274  XPLMCameraPosition_t prevCamera = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
276  float vCam_x = 0.0f, vCam_y = 0.0f, vCam_z = 0.0f;
278  float prevCamera_ts = 0.0f;
279 
281  int xpCycleNum = 0;
282 
283 protected:
286 public:
289 
290 public:
292  GlobVars (logLevelTy _logLvl = logINFO, bool _logMdlMatch = false) :
293  logLvl(_logLvl), bLogMdlMatch(_logMdlMatch) {}
295  void ReadConfigFile ();
297  void UpdateCfgVals ();
299  void ReadVersions ();
304  { xpThread = std::this_thread::get_id(); pluginId = XPLMGetMyID(); }
306  bool IsXPThread() const { return std::this_thread::get_id() == xpThread; }
307 
309  void UpdateCameraPos ();
310 };
311 
313 extern GlobVars glob;
314 
315 } // namespace XPMP2
316 
317 
318 
319 #endif
Definitions of 2-D routines, like drawing aircraft labels.
Definitions for serving TCAS target dataRef updates.
Additional definitions beyond what public XPCAircraft defines.
Managing CSL Models, including matching and loading.
Manages contrails for an aircraft.
Definitions for creating the map layer.
Low-level network communications, especially for TCP/UDP.
Reading of supporting text files:
Master/Client communication for aircraft synchronization on remote networked instances.
Sound for aircraft, based on XPLMSound.
Miscellaneous utility functions, including logging.
XPMP2::Aircraft / XPCAircraft represent an aircraft as managed by XPMP2.
#define UNKNOWN_PLUGIN_NAME
Definition: XPMP2.h:106
XPMP2::Aircraft represent an aircraft as managed by XPMP2.
Initialization and general control functions for XPMP2.
XPMPPlaneNotification
Events that trigger a notification callback.
Definition: XPMPMultiplayer.h:754
unsigned XPMPPlaneID
Unique ID for an aircraft created by a plugin.
Definition: XPMPMultiplayer.h:252
void(* XPMPPlaneNotifier_f)(XPMPPlaneID inPlaneID, XPMPPlaneNotification inNotification, void *inRefcon)
Type of the callback function you provide, called when one of the events defined in XPMPPlaneNotifica...
Definition: XPMPMultiplayer.h:766
Rendering functions.
Semi-public remote network functionality for master/client operations.
Actual representation of all aircraft in XPMP2.
Definition: XPMPAircraft.h:178
Definition: XPMPAircraft.h:70
logLevelTy
Logging level.
Definition: Utilities.h:249
@ logINFO
regular info messages
Definition: Utilities.h:251
std::map< XPMPPlaneID, Aircraft * > mapAcTy
Map of all aircraft, key is tthe plane id.
Definition: Aircraft.h:57
GlobVars glob
The one and only global variable structure.
Definition: Utilities.cpp:29
RemoteStatusTy
State of remote communcations.
Definition: XPMPRemote.h:439
@ REMOTE_RECV_WAITING
waiting to receive data, periodically sending a token of interest
Definition: XPMPRemote.h:443
@ REMOTE_OFF
no remote connectivtiy, not listening, not sending
Definition: XPMPRemote.h:440
@ REMOTE_SEND_WAITING
listening for a request to send data, but not actively sending data
Definition: XPMPRemote.h:441
@ REMOTE_SENDING
actively sending aircraft data out to the network
Definition: XPMPRemote.h:442
@ REMOTE_RECEIVING
actively receiving data
Definition: XPMPRemote.h:444
ThreeWaySwitchTy
3-way switch type
Definition: XPMP2.h:111
@ SWITCH_CFG_OFF
config: force off
Definition: XPMP2.h:112
@ SWITCH_CFG_AUTO
config: automatic or default
Definition: XPMP2.h:113
@ SWITCH_CFG_ON
config: force on
Definition: XPMP2.h:114
std::map< std::string, CSLModel > mapCSLModelTy
Map of CSLModels (owning the object), ordered by related group / type.
Definition: CSLModels.h:250
std::map< std::string, Doc8643 > mapDoc8643Ty
Map of Doc8643 information, key is the (icao) type code.
Definition: RelatedDoc8643.h:103
std::list< Obj8DataRefs > listObj8DataRefsTy
a list of Obj8DataRefs definitions
Definition: RelatedDoc8643.h:130
std::list< XPMPPlaneNotifierTy > listXPMPPlaneNotifierTy
Definition: XPMP2.h:129
std::map< std::string, XPLMMapLayerID > mapMapLayerIDTy
A map holding our layer identifier, indexed by the map's name.
Definition: Map.h:27
void XPMPSendNotification(const Aircraft &plane, XPMPPlaneNotification _notification)
Send a notification to all observers.
Definition: XPMPMultiplayer.cpp:572
std::map< std::string, std::string > mapCSLPackageTy
Map of CSLPackages: Maps an id to the base path (path ends on forward slash)
Definition: CSLModels.h:27
int PrefsFuncIntDefault(const char *, const char *, int _default)
Default config function just always returns the provided default value.
Definition: Utilities.cpp:179
All global config settings and variables are kept in one structure for convenient access and central ...
Definition: XPMP2.h:135
std::string defaultICAO
Default ICAO aircraft type designator if no match can be found.
Definition: XPMP2.h:180
listObj8DataRefsTy listObj8DataRefs
List of dataRef replacement in .obj files.
Definition: XPMP2.h:154
XPLMPluginID pluginId
This plugin's id.
Definition: XPMP2.h:267
bool bHasControlOfAIAircraft
Do we control X-Plane's AI/Multiplayer planes?
Definition: XPMP2.h:200
float vCam_z
Definition: XPMP2.h:276
bool bSoundMuteOnPause
Config: Mute on Pause?
Definition: XPMP2.h:250
bool bXPUsingModernGraphicsDriver
Using a modern graphics driver, ie. Vulkan/Metal?
Definition: XPMP2.h:263
listXPMPPlaneNotifierTy listObservers
List of notifier functions registered for being notified of creation/destruction/model change.
Definition: XPMP2.h:164
ThreeWaySwitchTy remoteCfgFromIni
Configuration file entry: Are we to support remote connections?
Definition: XPMP2.h:231
RemoteStatusTy remoteStatus
Status of remote connections to networked clients.
Definition: XPMP2.h:233
XPLMCameraPosition_t posCamera
Current camera location, updated every flight loop.
Definition: XPMP2.h:272
mapMapLayerIDTy mapMapLayers
Map of map layer ids, i.e. for each map this is the id of the corresponding layer of ours.
Definition: XPMP2.h:209
std::array< mapRelatedTy, REL_TXT_NUM > mapRelated
Content of related.txt and similar files as a map of keys to group id.
Definition: XPMP2.h:173
std::array< std::string, REL_TXT_NUM > pathRelated
Paths to related.txt, relOp.txt... files.
Definition: XPMP2.h:171
bool bDrawLabels
Shall we draw aircraft labels?
Definition: XPMP2.h:189
std::string pathMapIcons
path to file containing plane icons for map display
Definition: XPMP2.h:211
int contrailAltMax_ft
[ft] Auto Contrails: Maximum altitude
Definition: XPMP2.h:241
mapAcTy mapAc
Global map of all created planes.
Definition: XPMP2.h:187
int remotePort
The port we use is different from the port the X-Plane BEACON uses, so we don't get into conflict.
Definition: XPMP2.h:217
float prevCamera_ts
When has prevCamera been updated last?
Definition: XPMP2.h:278
bool bLabelCutOffAtVisibility
Cut off labels at XP's reported visibility mit?
Definition: XPMP2.h:195
std::string pluginName
Name of the plugin we are serving (used as map layer name or for folders)
Definition: XPMP2.h:157
std::string pathObj8DataRefs
Path to the Obj8DataRefs.txt file.
Definition: XPMP2.h:152
std::thread::id xpThread
id of X-Plane's thread (when it is OK to use XP API calls)
Definition: XPMP2.h:269
void UpdateCfgVals()
Update all settings, e.g. for logging level, by calling prefsFuncInt.
Definition: Utilities.cpp:185
std::string logAcronym
Plugin acronym used in log output.
Definition: XPMP2.h:159
ThreeWaySwitchTy eLabelOverride
Label drawing overriden in XPMP2.prf?
Definition: XPMP2.h:191
int(* prefsFuncInt)(const char *, const char *, int)
Configuration callback for integer values.
Definition: XPMP2.h:162
float vCam_x
Camera velocity.
Definition: XPMP2.h:276
float sndMasterVol
Sound master volume.
Definition: XPMP2.h:254
int verXPLM
XPLM's SDK version number (XPLMGetVersions)
Definition: XPMP2.h:261
void ThisThreadIsXP()
Set current thread as main xp Thread.
Definition: XPMP2.h:303
float maxLabelDist
Maximum distance for drawing labels? [m], defaults to 3nm.
Definition: XPMP2.h:193
int contrailAltMin_ft
[ft] Auto Contrails: Minimum altitude
Definition: XPMP2.h:240
std::string carIcaoType
Ground vehicle type identifier (map decides icon based on this)
Definition: XPMP2.h:182
size_t remoteBufSize
Buffer size, ie. max message length we send over multicast.
Definition: XPMP2.h:225
void ReadVersions()
Read version numbers into verXplane/verXPLM.
Definition: Utilities.cpp:280
std::string resourceDir
Resource directory, to store local definitions of vertical offsets (clamping)
Definition: XPMP2.h:184
bool bLogMdlMatch
Debug model matching?
Definition: XPMP2.h:141
bool remoteAutoDiscovery() const
Use auto-discovery for the MC send interface?
Definition: XPMP2.h:221
bool RemoteIsSender() const
Are we a sender?
Definition: XPMP2.h:237
XPMPPlaneID NextPlaneId()
Get the next unique artifical plane id.
Definition: Utilities.cpp:37
mapCSLModelTy mapCSLModels
Global map of all CSL Models, indexed by related group, aircraft type, and model id.
Definition: XPMP2.h:178
int verXPlane
X-Plane's version number (XPLMGetVersions)
Definition: XPMP2.h:259
logLevelTy logLvl
Logging level.
Definition: XPMP2.h:139
bool bObjReplDataRefs
Replace dataRefs in .obj files on load? (defaults to OFF!)
Definition: XPMP2.h:148
bool bHandleDupId
Handle duplicate XPMP2::Aircraft::modeS_id by overwriting with unique id.
Definition: XPMP2.h:145
bool bMapLabels
Do we show labels with the aircraft icons?
Definition: XPMP2.h:207
void ReadConfigFile()
Read from a generic XPMP2.prf or XPMP2.<logAcronym>.prf config file.
Definition: Utilities.cpp:79
bool bClampAll
Clamp all planes to the ground? Default is false as clamping is kinda expensive due to Y-Testing.
Definition: XPMP2.h:143
ThreeWaySwitchTy remoteCfg
Configuration: Are we to support remote connections?
Definition: XPMP2.h:229
XPLMCameraPosition_t prevCamera
Previous camera location of about a second ago.
Definition: XPMP2.h:274
bool bXPNetworkedSetup
Is X-Plane configured for networked multi-computer or multiplayer setup?
Definition: XPMP2.h:265
bool contrailMulti
Auto-create multiple or just a single contrail?
Definition: XPMP2.h:243
bool RemoteIsListener() const
Are we a listener?
Definition: XPMP2.h:235
float labelFontScaling
Label font scaling factor.
Definition: XPMP2.h:197
std::string remoteMCGroup
The multicast group that we use, which is the same X-Plane is using itself for its BEACON.
Definition: XPMP2.h:215
int remoteTxfFrequ
Max transfer frequency per second.
Definition: XPMP2.h:227
bool bSoundForceFmodInstance
Config: Force own FMOD instance?
Definition: XPMP2.h:248
std::string remoteSendIntf
The interface used to send multicast on. (Empty == auto-discovery)
Definition: XPMP2.h:219
void UpdateCameraPos()
Update the stored camera position and velocity values.
Definition: Utilities.cpp:295
mapDoc8643Ty mapDoc8643
Content of Doc8643.txt file.
Definition: XPMP2.h:169
ThreeWaySwitchTy eAIOverride
AI Control overriden in XPMP2.prf?
Definition: XPMP2.h:202
float vCam_y
Definition: XPMP2.h:276
bool bSoundAutoMuted
Is sound currently being auto-muted?
Definition: XPMP2.h:256
bool UsingModernGraphicsDriver() const
Using a modern graphics driver, ie. Vulkan/Metal?
Definition: XPMP2.h:301
int xpCycleNum
Current XP cycle number (see XPLMGetCycleNumber())
Definition: XPMP2.h:281
bool bSoundAvail
Is the sound system available?
Definition: XPMP2.h:252
bool bMapEnabled
Do we feed X-Plane's maps with our aircraft positions?
Definition: XPMP2.h:205
bool IsXPThread() const
Is this thread XP's main thread?
Definition: XPMP2.h:306
int contrailLifeTime
[s] Contrail default time to live
Definition: XPMP2.h:242
XPMPPlaneID planeId
Current plane ID counter.
Definition: XPMP2.h:285
GlobVars(logLevelTy _logLvl=logINFO, bool _logMdlMatch=false)
Constructor.
Definition: XPMP2.h:292
mapCSLPackageTy mapCSLPkgs
Global map of all CSL Packages, indexed by xsb_aircraft.txt::EXPORT_NAME
Definition: XPMP2.h:176
bool bSoundOnStartup
Config: Start Sound on startup?
Definition: XPMP2.h:246
std::string pathDoc8643
Path to Doc8643.txt file.
Definition: XPMP2.h:167
bool bObjReplTextures
Replace textures in .obj files on load if needed?
Definition: XPMP2.h:150
int remoteTTL
Time-to-live, or mumber of hops for a multicast message.
Definition: XPMP2.h:223
Stores the function and refcon pointer for plane creation/destrcution notifications.
Definition: XPMP2.h:118
bool operator==(const XPMPPlaneNotifierTy &o)
Definition: XPMP2.h:125
XPMPPlaneNotifier_f func
Definition: XPMP2.h:119
XPMPPlaneNotifierTy(XPMPPlaneNotifier_f _func=nullptr, void *_refcon=nullptr)
Definition: XPMP2.h:122
void * refcon
Definition: XPMP2.h:120