Repository navigation
Expand file tree
/
Copy pathautodgs.cpp
More file actions
482 lines (400 loc) · 17.1 KB
/
Copy pathautodgs.cpp
File metadata and controls
482 lines (400 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//
// AutoDGS: Show Marshaller or VDGS at default airports
//
// Copyright (C) 2006-2013 Jonathan Harris
// Copyright (C) 2023, 2025 Holger Teutsch
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA
//
// This code loosely follows
// Google's style guide: https://google.github.io/styleguide/cppguide.html
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <fstream>
#include <map>
#include <algorithm>
#include <filesystem>
#include "autodgs.h"
#include "airport.h"
#include "plane.h"
#include "XPLMPlugin.h"
#include "XPLMProcessing.h"
#include "XPLMMenus.h"
#include "version.h"
const char *log_msg_prefix = "AutoDGS: ";
const char * const opmode_str[] = { "Automatic", "Manual" };
// Globals
bool error_disabled;
std::string xp_dir;
std::string base_dir; // base directory of AutoDGS
std::string sys_cfg_dir; // <base_dir>/cfg
std::string user_cfg_dir; // <xp_dir>/Output/AutoDGS
opmode_t operation_mode = MODE_AUTO;
XPLMCommandRef cycle_dgs_cmdr, move_dgs_closer_cmdr, activate_cmdr,
toggle_ui_cmdr, toggle_jetway_cmdr;
XPLMDataRef plane_x_dr, plane_y_dr, plane_z_dr, is_helicopter_dr, y_agl_dr;
XPLMDataRef plane_lat_dr, plane_lon_dr, plane_elevation_dr, plane_true_psi_dr;
XPLMDataRef gear_fnrml_dr, acf_cg_y_dr, acf_cg_z_dr, gear_z_dr;
XPLMDataRef beacon_dr, parkbrake_dr, acf_icao_dr, total_running_time_sec_dr;
XPLMDataRef percent_lights_dr, ev100_dr, xp_version_dr, eng_running_dr, sin_wave_dr;
XPLMDataRef vr_enabled_dr, ground_speed_dr;
static XPLMDataRef zulu_time_minutes_dr, zulu_time_hours_dr;
XPLMProbeRef probe_ref;
XPLMObjectRef dgs_obj[2], pole_base_obj;
static fem::LLPos plane_pos; // current plane position, updated in FlightLoopCb()
static fem::LLPos plane_pos_prev; // previous plane position
// track reference frame generation number
static XPLMDataRef lat_ref_dr, lon_ref_dr;
static float lat_ref, lon_ref;
int ref_gen = 1;
float now; // current timestamp
int on_ground;
std::unique_ptr<Airport> arpt;
// keep exactly the same order as in enum _DGS_DREF
const char *dgs_dlist_dr[] = {
"AutoDGS/dgs/status",
"AutoDGS/dgs/lr",
"AutoDGS/dgs/track",
"AutoDGS/dgs/xtrack",
"AutoDGS/dgs/distance",
"AutoDGS/dgs/distance_0",
"AutoDGS/dgs/distance_01",
"AutoDGS/dgs/icao_0",
"AutoDGS/dgs/icao_1",
"AutoDGS/dgs/icao_2",
"AutoDGS/dgs/icao_3",
"AutoDGS/dgs/r1_scroll",
"AutoDGS/dgs/r1c0",
"AutoDGS/dgs/r1c1",
"AutoDGS/dgs/r1c2",
"AutoDGS/dgs/r1c3",
"AutoDGS/dgs/r1c4",
"AutoDGS/dgs/r1c5",
"AutoDGS/dgs/boarding",
"AutoDGS/dgs/paxno_0",
"AutoDGS/dgs/paxno_1",
"AutoDGS/dgs/paxno_2",
NULL
};
static float time_utc_m0, time_utc_m1, time_utc_h0, time_utc_h1, vdgs_brightness;
static float FlightLoopCb(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter,
void* inRefcon);
static XPLMCreateFlightLoop_t flight_loop_ctx = {sizeof(XPLMCreateFlightLoop_t),
xplm_FlightLoop_Phase_BeforeFlightModel, FlightLoopCb, nullptr};
static XPLMFlightLoopID flight_loop_id;
static bool pending_plane_loaded_cb = false; // delayed init
//------------------------------------------------------------------------------------
// set mode to arrival
void Activate(void) {
if (!on_ground) {
LogMsg("can't set active when not on ground");
return;
}
if (arpt && arpt->state() > Airport::INACTIVE)
return;
plane.ResetBeacon();
std::string airport_id = AptAirport::LocateAirport(plane_pos);
if (!airport_id.empty()) {
LogMsg("now on airport: %s", airport_id.c_str());
if (arpt == nullptr || arpt->name() != airport_id) { // don't reload same
arpt = Airport::LoadAirport(airport_id);
}
} else {
LogMsg("airport could not be identified at %0.8f,%0.8f", plane_pos.lat, plane_pos.lon);
arpt = nullptr;
}
if (arpt == nullptr)
return;
arpt->ResetState(plane.BeaconOn() ? Airport::ARRIVAL : Airport::INACTIVE);
LogMsg("airport loaded: '%s', new state: %s", arpt->name().c_str(), Airport::state_str[arpt->state()]);
UpdateUI();
}
// Dataref accessor, only called for the _utc_ datarefs
static float GetDgsFloat(void* ref) {
if (ref == nullptr)
return -1.0f;
return *(float*)ref;
}
static float FlightLoopCb(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter,
void* inRefcon) {
static float on_ground_ts; // debounce ground contact
try {
if (pending_plane_loaded_cb) {
plane.PlaneLoadedCb();
pending_plane_loaded_cb = false;
}
plane_pos_prev = plane_pos;
plane_pos = fem::LLPos(XPLMGetDataf(plane_lat_dr), XPLMGetDataf(plane_lon_dr));
// if we go 3 * supersonic it's a teleportation, e.g. a ToLiss situation reload
if (fem::len(plane_pos - plane_pos_prev) > inElapsedSinceLastCall * 3.0f * 340.0f) {
LogMsg("teleportation detected, resetting airport");
arpt = nullptr;
}
float loop_delay = 2.0;
now = XPLMGetDataf(total_running_time_sec_dr);
int og;
if (plane.is_helicopter)
og = (XPLMGetDataf(y_agl_dr) < 10.0);
else
og = (XPLMGetDataf(gear_fnrml_dr) != 0.0);
if (og != on_ground && now > on_ground_ts + 10.0) {
on_ground = og;
on_ground_ts = now;
LogMsg("transition to on_ground: %d", on_ground);
if (on_ground) {
if (operation_mode == MODE_AUTO)
Activate();
} else {
// transition to airborne
arpt = nullptr;
}
}
if (arpt)
loop_delay = arpt->StateMachine();
// update global dataref values
// brightness for VDGS
static constexpr float min_brightness = 0.025; // relative to 1
if (ev100_dr) {
// if ev100 is available, we use it to set brightness
static constexpr float kMinEv100 = 6.0f;
static constexpr float kMaxEv100 = 11.0f;
float ev100 = XPLMGetDataf(ev100_dr);
ev100 = std::clamp(ev100, kMinEv100, kMaxEv100);
const float f = (ev100 - kMinEv100) / (kMaxEv100 - kMinEv100);
// ev100 is logarithmic and vdgs_brightness linear, so we use exp here
const float exp_f = (std::exp(f) - 1.0f) / (std::exp(1.0f) - 1.0f);
vdgs_brightness = min_brightness + (1.0f - min_brightness) * exp_f;
// LogMsg("ev100: %0.2f, vdgs_brightness: %0.3f", ev100, vdgs_brightness);
} else {
// fallback: use percent_lights_on
vdgs_brightness =
min_brightness + (1.0f - min_brightness) * std::pow(1.0f - XPLMGetDataf(percent_lights_dr), 6.0f);
}
// UTC time digits
int zm = XPLMGetDatai(zulu_time_minutes_dr);
int zh = XPLMGetDatai(zulu_time_hours_dr);
time_utc_m0 = zm % 10;
time_utc_m1 = zm / 10;
time_utc_h0 = zh % 10;
time_utc_h1 = zh / 10;
return loop_delay;
} catch (const std::exception& ex) {
LogMsg("fatal error: '%s'", ex.what()); // hopefully LogMsg is still alive
LogMsg("disabling plugin to avoid further errors and hopefully protect X-Plane from crashing");
error_disabled = true;
return 0;
}
}
// call backs for commands
static int CmdCb(XPLMCommandRef cmdr, XPLMCommandPhase phase, [[maybe_unused]] void* ref) {
if (error_disabled)
return 0;
if (xplm_CommandBegin != phase)
return 0;
if (cmdr == cycle_dgs_cmdr) {
if (arpt)
arpt->CycleDgsType();
UpdateUI();
} else if (cmdr == activate_cmdr) {
LogMsg("cmd manually_activate");
Activate();
} else if (cmdr == move_dgs_closer_cmdr) {
if (arpt)
arpt->DgsMoveCloser();
} else if (cmdr == toggle_ui_cmdr) {
LogMsg("cmd toggle_ui");
ToggleUI();
}
return 0;
}
// call back for menu
static void MenuCb([[maybe_unused]] void* menu_ref, void* item_ref) {
if (error_disabled)
return;
XPLMCommandOnce(*(XPLMCommandRef*)item_ref);
}
// check for shift of reference frame
void CheckRefFrameShift() {
// check for shift of reference frame
float lat_r = XPLMGetDataf(lat_ref_dr);
float lon_r = XPLMGetDataf(lon_ref_dr);
if (lat_r != lat_ref || lon_r != lon_ref) {
lat_ref = lat_r;
lon_ref = lon_r;
ref_gen++;
LogMsg("reference frame shift");
}
}
// =========================== plugin entry points ===============================================
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) {
strcpy(outName, "AutoDGS " VERSION);
strcpy(outSig, "hotbso.AutoDGS");
strcpy(outDesc, "Automatically provides DGS for gateway airports");
LogMsg("startup " VERSION);
XPLMEnableFeature("XPLM_USE_NATIVE_PATHS", 1);
XPLMEnableFeature("XPLM_USE_NATIVE_WIDGET_WINDOWS", 1);
char buffer[2048];
XPLMGetSystemPath(buffer);
xp_dir = std::string(buffer);
// set plugin's base dir
base_dir = xp_dir + "Resources/plugins/AutoDGS/";
sys_cfg_dir = base_dir + "cfg/";
user_cfg_dir = xp_dir + "Output/AutoDGS/";
std::filesystem::create_directories(user_cfg_dir);
if (!AptAirport::CollectAirports(xp_dir)) {
LogMsg("init failure: Can't load airports");
return 0;
}
// Datarefs
xp_version_dr = XPLMFindDataRef("sim/version/xplane_internal_version");
plane_x_dr = XPLMFindDataRef("sim/flightmodel/position/local_x");
plane_y_dr = XPLMFindDataRef("sim/flightmodel/position/local_y");
plane_z_dr = XPLMFindDataRef("sim/flightmodel/position/local_z");
gear_fnrml_dr = XPLMFindDataRef("sim/flightmodel/forces/fnrml_gear");
plane_lat_dr = XPLMFindDataRef("sim/flightmodel/position/latitude");
plane_lon_dr = XPLMFindDataRef("sim/flightmodel/position/longitude");
plane_elevation_dr = XPLMFindDataRef("sim/flightmodel/position/elevation");
plane_true_psi_dr = XPLMFindDataRef("sim/flightmodel2/position/true_psi");
parkbrake_dr = XPLMFindDataRef("sim/flightmodel/controls/parkbrake");
beacon_dr = XPLMFindDataRef("sim/cockpit2/switches/beacon_on");
eng_running_dr = XPLMFindDataRef("sim/flightmodel/engine/ENGN_running");
acf_icao_dr = XPLMFindDataRef("sim/aircraft/view/acf_ICAO");
acf_cg_y_dr = XPLMFindDataRef("sim/aircraft/weight/acf_cgY_original");
acf_cg_z_dr = XPLMFindDataRef("sim/aircraft/weight/acf_cgZ_original");
gear_z_dr = XPLMFindDataRef("sim/aircraft/parts/acf_gear_znodef");
is_helicopter_dr = XPLMFindDataRef("sim/aircraft2/metadata/is_helicopter");
y_agl_dr = XPLMFindDataRef("sim/flightmodel2/position/y_agl");
total_running_time_sec_dr = XPLMFindDataRef("sim/time/total_running_time_sec");
percent_lights_dr = XPLMFindDataRef("sim/graphics/scenery/percent_lights_on");
sin_wave_dr = XPLMFindDataRef("sim/graphics/animation/sin_wave_2");
ground_speed_dr = XPLMFindDataRef("sim/flightmodel/position/groundspeed");
zulu_time_minutes_dr = XPLMFindDataRef("sim/cockpit2/clock_timer/zulu_time_minutes");
zulu_time_hours_dr = XPLMFindDataRef("sim/cockpit2/clock_timer/zulu_time_hours");
lat_ref_dr = XPLMFindDataRef("sim/flightmodel/position/lat_ref");
lon_ref_dr = XPLMFindDataRef("sim/flightmodel/position/lon_ref");
// these are served via instancing
for (int i = 0; i < DGS_DR_NUM; i++)
XPLMRegisterDataAccessor(dgs_dlist_dr[i], xplmType_Float, 0, NULL, NULL, GetDgsFloat, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, (void*)0, 0);
// these are served globally
XPLMRegisterDataAccessor("AutoDGS/dgs/time_utc_m0", xplmType_Float, 0, NULL, NULL, GetDgsFloat, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, &time_utc_m0, 0);
XPLMRegisterDataAccessor("AutoDGS/dgs/time_utc_m1", xplmType_Float, 0, NULL, NULL, GetDgsFloat, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, &time_utc_m1, 0);
XPLMRegisterDataAccessor("AutoDGS/dgs/time_utc_h0", xplmType_Float, 0, NULL, NULL, GetDgsFloat, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, &time_utc_h0, 0);
XPLMRegisterDataAccessor("AutoDGS/dgs/time_utc_h1", xplmType_Float, 0, NULL, NULL, GetDgsFloat, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, &time_utc_h1, 0);
XPLMRegisterDataAccessor("AutoDGS/dgs/vdgs_brightness", xplmType_Float, 0, NULL, NULL, GetDgsFloat, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, &vdgs_brightness, 0);
create_api_drefs();
int is_XP11 = (XPLMGetDatai(xp_version_dr) < 120000);
const char* obj_name[2];
if (is_XP11) {
LogMsg("XP11 detected");
obj_name[0] = "Marshaller_XP11.obj";
obj_name[1] = "Safedock-T2-24-pole_XP11.obj";
} else {
obj_name[0] = "Marshaller.obj";
obj_name[1] = "Safedock-T2-24-pole.obj";
}
for (int i = 0; i < 2; i++) {
std::string on = base_dir + "resources/" + obj_name[i];
dgs_obj[i] = XPLMLoadObject(on.c_str());
if (dgs_obj[i] == NULL) {
LogMsg("error loading obj: %s", on.c_str());
return 0;
}
}
std::string on = base_dir + "resources/pole_base.obj";
pole_base_obj = XPLMLoadObject(on.c_str());
if (pole_base_obj == NULL) {
LogMsg("error loading obj: %s", on.c_str());
return 0;
}
// own commands
cycle_dgs_cmdr = XPLMCreateCommand("AutoDGS/cycle_dgs", "Cycle DGS between Marshaller, VDGS");
XPLMRegisterCommandHandler(cycle_dgs_cmdr, CmdCb, 0, NULL);
move_dgs_closer_cmdr = XPLMCreateCommand("AutoDGS/move_dgs_closer", "Move DGS closer by 2m");
XPLMRegisterCommandHandler(move_dgs_closer_cmdr, CmdCb, 0, NULL);
activate_cmdr = XPLMCreateCommand("AutoDGS/activate", "Manually activate searching for stands_");
XPLMRegisterCommandHandler(activate_cmdr, CmdCb, 0, NULL);
toggle_ui_cmdr = XPLMCreateCommand("AutoDGS/toggle_ui", "Open UI");
XPLMRegisterCommandHandler(toggle_ui_cmdr, CmdCb, 0, NULL);
// menu
XPLMMenuID menu = XPLMFindPluginsMenu();
int sub_menu = XPLMAppendMenuItem(menu, "AutoDGS", NULL, 1);
XPLMMenuID adgs_menu = XPLMCreateMenu("AutoDGS", menu, sub_menu, MenuCb, NULL);
XPLMAppendMenuItem(adgs_menu, "Manually activate", &activate_cmdr, 0);
XPLMAppendMenuItem(adgs_menu, "Cycle DGS", &cycle_dgs_cmdr, 0);
XPLMAppendMenuItem(adgs_menu, "Move DGS closer by 2m", &move_dgs_closer_cmdr, 0);
XPLMAppendMenuItem(adgs_menu, "Toggle UI", &toggle_ui_cmdr, 0);
// foreign commands
toggle_jetway_cmdr = XPLMFindCommand("sim/ground_ops/jetway");
flight_loop_id = XPLMCreateFlightLoop(&flight_loop_ctx);
return 1;
}
PLUGIN_API void XPluginStop(void) {
XPLMUnregisterFlightLoopCallback(FlightLoopCb, NULL);
for (int i = 0; i < 2; i++)
if (dgs_obj[i])
XPLMUnloadObject(dgs_obj[i]);
if (pole_base_obj)
XPLMUnloadObject(pole_base_obj);
}
PLUGIN_API int XPluginEnable(void) {
if (error_disabled)
return 0;
probe_ref = XPLMCreateProbe(xplm_ProbeY);
UpdateUI(false); // in case we reenable
return 1;
}
PLUGIN_API void XPluginDisable(void) {
arpt = nullptr;
if (probe_ref)
XPLMDestroyProbe(probe_ref);
}
PLUGIN_API void XPluginReceiveMessage([[maybe_unused]] XPLMPluginID in_from, long in_msg, void* in_param) {
if (error_disabled)
return;
// my plane loaded
if (in_msg == XPLM_MSG_PLANE_LOADED && in_param == 0) {
LogMsg("plane loaded, resetting airport");
arpt = nullptr;
on_ground = 0;
XPLMScheduleFlightLoop(flight_loop_id, 0, 0);
pending_plane_loaded_cb = true;
XPLMScheduleFlightLoop(flight_loop_id, 15.0, 1); // let the dust settle
return;
}
if (in_msg == XPLM_MSG_SCENERY_LOADED) {
// private datarefs are usually intialized late
if (ev100_dr == nullptr) {
ev100_dr = XPLMFindDataRef("sim/private/controls/photometric/ev100");
if (ev100_dr)
LogMsg("ev100 dataref mapped");
}
return;
}
if (in_msg == XPLM_MSG_AIRPORT_LOADED) {
LogMsg("airport loaded message received, resetting airport");
arpt = nullptr;
return;
}
}