-
Notifications
You must be signed in to change notification settings - Fork 2
/
openSAM.cpp
838 lines (681 loc) · 25.6 KB
/
openSAM.cpp
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*
openSAM: open source SAM emulator for X Plane
Copyright (C) 2024 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
*/
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <fstream>
#include "openSAM.h"
#include "os_dgs.h"
#include "os_jw.h"
#include "os_anim.h"
#include "XPLMPlanes.h"
/*
* On the various coordinate systems and angles:
*
* Objects are drawn in a +x east , -z true north, +y up system.
* Headings (hdgt) are measured from -z (=true north) right turning
*
* Imagine looking from below to the sky you have the more traditional 'math' view
* +x right, +z up, angles left turning from the +x axis to the +z axis.
*
* So if alpha is a 'math' angle we have
*
* hdgt = 90° + alpha.
*
* If we have a coordinate system that is rotated by an angle of psi we have
*
* hdgt\rot = hdgt - psi
*
* When it comes to object rotations or longitudes we use relative angles in (-180, +180]
* This is done by RA().
*
* So first of all we rotate and shift everything into the 'door' frame i.e.
* acf nose pointing up to -z, door at (0,0), jetways to the left somewhere at -x and some z.
*
* These values are kept in the "active_jw".
*
* Then we do our math in the door frame and transform everything back to the jetway frame
* to get the rotation angles.
*
* The datarefs for jetway animation are:
* rotation1 tunnel relative to the placed object
* rotation2 cabin relative to the tunnel
* rotation3 tunnel relative to horizontal (= x-z plane)
* wheelrotatec wheel base relative to tunnel around the y-axis
* wheelrotater right wheel
* wheelrotatel left wheel
* wheel delta height in m of tunnel over wheelbase relative to horizontal
*
* Likewise for DGS we xform everything into the stand frame and go from there.
*
*/
static int init_done, init_fail;
static std::string xp_dir;
static std::string pref_path;
static XPLMMenuID seasons_menu;
static int auto_item, season_item[4];
static int auto_season;
static int airport_loaded;
static int nh; // on northern hemisphere
static int season; // 0-3
static const char *dr_name[] = {"sam/season/winter", "sam/season/spring",
"sam/season/summer", "sam/season/autumn"};
static int sam_library_installed;
XPLMDataRef date_day_dr,
plane_x_dr, plane_y_dr, plane_z_dr, plane_lat_dr, plane_lon_dr, plane_elevation_dr,
plane_true_psi_dr, plane_y_agl_dr, lat_ref_dr, lon_ref_dr, is_helicopter_dr,
draw_object_x_dr, draw_object_y_dr, draw_object_z_dr, draw_object_psi_dr, parkbrake_dr,
beacon_dr, eng_running_dr, acf_icao_dr, acf_cg_y_dr, acf_cg_z_dr, acf_gear_z_dr,
acf_door_x_dr, acf_door_y_dr, acf_door_z_dr, acf_livery_path,
gear_fnrml_dr,
total_running_time_sec_dr,
vr_enabled_dr;
int on_ground = 1;
static float on_ground_ts;
float lat_ref = -1000, lon_ref = -1000;
/* generation # of reference frame
* init with 1 so jetways never seen by the accessor won't be considered in find_dockable_jws() */
unsigned int ref_gen = 1;
int auto_select_jws;
float parked_x, parked_z;
int parked_ngen;
float now; // current timestamp
std::string base_dir; // base directory of openSAM
int beacon_state, beacon_last_pos; // beacon state, last switch_pos, ts of last switch actions
float beacon_off_ts, beacon_on_ts;
int use_engine_running; // instead of beacon, e.g. MD11
int dont_connect_jetway; // e.g. for ZIBO with own ground service
int is_helicopter;
static float plane_cg_y;
std::map<std::string, DoorInfo> door_info_map;
int n_door;
DoorInfo door_info[MAX_DOOR];
std::string acf_icao;
unsigned long long stat_sc_far_skip, stat_far_skip, stat_near_skip,
stat_acc_called, stat_jw_match, stat_dgs_acc, stat_dgs_acc_last,
stat_anim_acc_called, stat_auto_drf_called;
XPLMProbeInfo_t probeinfo;
XPLMProbeRef probe_ref;
XPLMMenuID anim_menu;
static void
save_pref()
{
FILE *f = fopen(pref_path.c_str(), "w");
if (NULL == f)
return;
// encode southern hemisphere with negative season
int s = nh ? season : -season;
fprintf(f, "%d,%d,%d", auto_season, s, auto_select_jws);
fclose(f);
log_msg("Saving pref auto_season: %d, season: %d, auto_select_jws: %d", auto_season, s, auto_select_jws);
}
static void
load_pref()
{
// set some reasonable default values in case there is no pref file
nh = 1;
auto_season = 1;
season = 1;
auto_select_jws = 1;
FILE *f = fopen(pref_path.c_str(), "r");
if (NULL == f)
return;
fscanf(f, "%i,%i,%i", &auto_season, &season, &auto_select_jws);
log_msg("From pref: auto_season: %d, seasons: %d, auto_select_jws: %d",
auto_season, season, auto_select_jws);
fclose(f);
if (season < 0) {
nh = 0;
season = -season;
}
}
int
check_beacon(void)
{
if (use_engine_running) {
int er[8];
int n = XPLMGetDatavi(eng_running_dr, er, 0, 8);
for (int i = 0; i < n; i++)
if (er[i])
return 1;
return 0;
}
/* when checking the beacon guard against power transitions when switching
to the APU generator (e.g. for the ToLiss fleet).
Report only state transitions when the new state persisted for 3 seconds */
int beacon = XPLMGetDatai(beacon_dr);
if (beacon) {
if (! beacon_last_pos) {
beacon_on_ts = now;
beacon_last_pos = 1;
} else if (now > beacon_on_ts + 3.0)
beacon_state = 1;
} else {
if (beacon_last_pos) {
beacon_off_ts = now;
beacon_last_pos = 0;
} else if (now > beacon_off_ts + 3.0)
beacon_state = 0;
}
return beacon_state;
}
// Accessor for the "opensam/SAM_Library_installed" dataref
static int
sam_lib_installed_acc(void *ref)
{
UNUSED(ref);
return sam_library_installed;
}
// Accessor for the "sam/season/*" datarefs
static int
read_season_acc(void *ref)
{
int s = (long long)ref;
int val = (s == season);
//log_msg("accessor %s called, returns %d", dr_name[s], val);
return val;
}
static int
cmd_activate_cb(XPLMCommandRef cmdr, XPLMCommandPhase phase, void *ref)
{
UNUSED(cmdr);
UNUSED(ref);
if (xplm_CommandBegin != phase)
return 0;
log_msg("cmd manually_activate");
dgs_set_active();
return 0;
}
static int
cmd_toggle_ui_cb(XPLMCommandRef cmdr, XPLMCommandPhase phase, void *ref)
{
UNUSED(cmdr);
UNUSED(ref);
if (xplm_CommandBegin != phase)
return 0;
log_msg("cmd toggle_ui");
toggle_ui();
return 0;
}
int
check_teleportation()
{
if (! on_ground)
return 0;
float x = XPLMGetDataf(plane_x_dr);
float z = XPLMGetDataf(plane_z_dr);
int ngen = ref_gen;
if (parked_ngen != ngen || fabsf(parked_x - x) > 1.0f || fabsf(parked_z - z) > 1.0f) {
log_msg("parked_ngen: %d, ngen: %d, parked_x: %0.3f, x: %0.3f, parked_z: %0.3f, z: %0.3f",
parked_ngen, ngen, parked_x, x, parked_z, z);
return 1;
}
return 0;
}
static float
flight_loop_cb(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop, int inCounter,
void *inRefcon)
{
UNUSED(inElapsedSinceLastCall);
UNUSED(inElapsedTimeSinceLastFlightLoop);
UNUSED(inCounter);
UNUSED(inRefcon);
static float jw_next_ts, dgs_next_ts, anim_next_ts;
now = XPLMGetDataf(total_running_time_sec_dr);
int og = (XPLMGetDataf(gear_fnrml_dr) != 0.0);
if (og != on_ground && now > on_ground_ts + 10.0f) {
on_ground = og;
on_ground_ts = now;
log_msg("transition to on_ground: %d", on_ground);
if (on_ground)
dgs_set_active();
else
dgs_set_inactive();
}
float jw_loop_delay = jw_next_ts - now;
float dgs_loop_delay = dgs_next_ts - now;
float anim_loop_delay = anim_next_ts - now;
if (! is_helicopter) {
if (jw_loop_delay <= 0.0f) {
jw_loop_delay = jw_state_machine();
jw_next_ts = now + jw_loop_delay;
}
if (dgs_loop_delay <= 0.0f) {
dgs_loop_delay = dgs_state_machine();
dgs_next_ts = now + dgs_loop_delay;
}
}
if (anim_loop_delay <= 0.0f) {
anim_loop_delay = anim_state_machine();
anim_next_ts = now + anim_loop_delay;
}
return std::min(anim_loop_delay, std::min(jw_loop_delay, dgs_loop_delay));
}
// set season according to date
static void
set_season_auto()
{
if (! auto_season)
return;
int day = XPLMGetDatai(date_day_dr);
if (nh) {
if (day <= 80) {
season = 0;
} else if (day <= 172) {
season = 1;
} else if (day <= 264) {
season = 2;
} else if (day <= 355) {
season = 3;
} else if (day) {
season = 0;
}
} else {
if (day <= 80) {
season = 2;
} else if (day <= 172) {
season = 3;
} else if (day <= 264) {
season = 0;
} else if (day <= 355) {
season = 1;
} else if (day) {
season = 2;
}
}
log_msg("nh: %d, day: %d, season: %d", nh, day, season);
}
// emulate a kind of radio buttons
static void
set_menu()
{
XPLMCheckMenuItem(seasons_menu, auto_item,
auto_season ? xplm_Menu_Checked : xplm_Menu_Unchecked);
XPLMCheckMenuItem(seasons_menu, season_item[season], xplm_Menu_Checked);
for (int i = 0; i < 4; i++)
if (i != season)
XPLMCheckMenuItem(seasons_menu, season_item[i], xplm_Menu_Unchecked);
}
static void
menu_cb(void *menu_ref, void *item_ref)
{
UNUSED(menu_ref);
int entry = (long long)item_ref;
if (entry == 4) {
auto_season = !auto_season;
set_season_auto();
} else {
season = entry;
auto_season = 0; // selecting a season always goes to manual mode
}
set_menu();
save_pref();
}
static bool
load_door_info(const std::string& fn)
{
bool res = false;
std::string line;
std::ifstream f(fn);
if (f.is_open()) {
log_msg("Building door_info_map from %s", fn.c_str());
while (std::getline(f, line)) {
size_t i = line.find('\r');
if (i != std::string::npos)
line.resize(i);
char icao[5];
int d;
float x, y, z;
if (5 == sscanf(line.c_str(), "%4s %d %f %f %f", icao, &d, &x, &y, &z)) {
if (icao[0] == '#')
continue;
if (d < 1 || d > MAX_DOOR) {
log_msg("invalid entry: '%s'", line.c_str());
continue;
}
char c = d + '0';
DoorInfo d{x, y, z};
door_info_map[std::string(icao) + c] = d;
}
}
f.close();
res = true;
}
return res;
}
static bool
find_icao_in_file(const std::string& acf_icao, const std::string& fn, std::string& line)
{
bool res = false;
std::ifstream f(fn);
if (f.is_open()) {
log_msg("check whether acf '%s' is in file %s", acf_icao.c_str(), fn.c_str());
while (std::getline(f, line)) {
size_t i = line.find('\r');
if (i != std::string::npos)
line.resize(i);
if (line.find(acf_icao) == 0) {
log_msg("found acf %s in %s", acf_icao.c_str(), fn.c_str());
res = true;
break;
}
}
f.close();
}
return res;
}
// =========================== plugin entry points ===============================================
PLUGIN_API int
XPluginStart(char *out_name, char *out_sig, char *out_desc)
{
log_msg("Startup " VERSION);
probeinfo.structSize = sizeof(XPLMProbeInfo_t);
strcpy(out_name, "openSAM " VERSION);
strcpy(out_sig, "openSAM.hotbso");
strcpy(out_desc, "A plugin that emulates SAM");
// Always use Unix-native paths on the Mac!
XPLMEnableFeature("XPLM_USE_NATIVE_PATHS", 1);
XPLMEnableFeature("XPLM_USE_NATIVE_WIDGET_WINDOWS", 1);
char buffer[2048];
XPLMGetSystemPath(buffer);
xp_dir = std::string(buffer);
// set pref path
XPLMGetPrefsPath(buffer);
XPLMExtractFileAndPath(buffer);
pref_path = std::string(buffer) + "/openSAM.prf";
// get my base dir
base_dir = xp_dir + "Resources/plugins/openSAM/";
if (!load_door_info(base_dir + "acf_door_position.txt")) {
log_msg("Error loading acf_door_position.txt, bye!");
return 0;
}
date_day_dr = XPLMFindDataRef("sim/time/local_date_days");
lat_ref_dr = XPLMFindDataRef("sim/flightmodel/position/lat_ref");
lon_ref_dr = XPLMFindDataRef("sim/flightmodel/position/lon_ref");
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");
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");
plane_y_agl_dr = XPLMFindDataRef("sim/flightmodel2/position/y_agl");
is_helicopter_dr = XPLMFindDataRef("sim/aircraft2/metadata/is_helicopter");
draw_object_x_dr = XPLMFindDataRef("sim/graphics/animation/draw_object_x");
draw_object_y_dr = XPLMFindDataRef("sim/graphics/animation/draw_object_y");
draw_object_z_dr = XPLMFindDataRef("sim/graphics/animation/draw_object_z");
draw_object_psi_dr = XPLMFindDataRef("sim/graphics/animation/draw_object_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");
gear_fnrml_dr = XPLMFindDataRef("sim/flightmodel/forces/fnrml_gear");
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");
acf_gear_z_dr = XPLMFindDataRef("sim/aircraft/parts/acf_gear_znodef");
acf_door_x_dr = XPLMFindDataRef("sim/aircraft/view/acf_door_x");
acf_door_y_dr = XPLMFindDataRef("sim/aircraft/view/acf_door_y");
acf_door_z_dr = XPLMFindDataRef("sim/aircraft/view/acf_door_z");
acf_livery_path = XPLMFindDataRef("sim/aircraft/view/acf_livery_path");
total_running_time_sec_dr = XPLMFindDataRef("sim/time/total_running_time_sec");
vr_enabled_dr = XPLMFindDataRef("sim/graphics/VR/enabled");
load_pref();
// collect all *.xml files
try {
SceneryPacks scp(xp_dir);
collect_sam_xml(scp);
log_msg("%d sceneries with sam jetways found", (int)sceneries.size());
} catch (const OsEx& ex) {
log_msg(ex.what());
return 0; // bye
}
// if commands or dataref accessors are already registered it's to late to
// fail XPluginStart as the dll is unloaded and X-Plane crashes
// so more initialization in XPluginEnable
return 1;
}
PLUGIN_API void
XPluginStop(void)
{
}
PLUGIN_API void
XPluginDisable(void)
{
if (probe_ref)
XPLMDestroyProbe(probe_ref);
save_pref();
log_msg("acc called: %llu", stat_acc_called);
log_msg("scenery far skip: %llu", stat_sc_far_skip);
log_msg("far skip: %llu", stat_far_skip);
log_msg("near skip: %llu", stat_near_skip);
log_msg("dgs acc called: %llu", stat_dgs_acc);
log_msg("last_dgs acc: %llu", stat_dgs_acc_last);
log_msg("stat_anim_acc_called: %llu", stat_anim_acc_called);
log_msg("stat_auto_drf_called: %llu", stat_auto_drf_called);
}
PLUGIN_API int
XPluginEnable(void)
{
if (init_fail) // once and for all
goto fail;
if (!init_done) {
init_done = 1;
XPLMRegisterDataAccessor("opensam/SAM_Library_installed", xplmType_Int, 0, sam_lib_installed_acc,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL);
// create the seasons datarefs
for (int i = 0; i < 4; i++)
XPLMRegisterDataAccessor(dr_name[i], xplmType_Int, 0, read_season_acc,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, (void *)(long long)i, NULL);
// own commands
XPLMCommandRef activate_cmdr = XPLMCreateCommand("openSAM/activate", "Manually activate searching for DGS");
XPLMRegisterCommandHandler(activate_cmdr, cmd_activate_cb, 0, NULL);
XPLMCommandRef toggle_ui_cmdr = XPLMCreateCommand("openSAM/toggle_ui", "Toggle UI");
XPLMRegisterCommandHandler(toggle_ui_cmdr, cmd_toggle_ui_cb, 0, NULL);
if (!jw_init() || !dgs_init() || !anim_init()) {
log_msg("initialization of a sub-module failed!");
goto fail;
}
// build menues
XPLMMenuID menu = XPLMFindPluginsMenu();
XPLMMenuID os_menu = XPLMCreateMenu("openSAM", menu,
XPLMAppendMenuItem(menu, "openSAM", NULL, 0),
NULL, NULL);
// openSAM
XPLMAppendMenuItemWithCommand(os_menu, "Dock Jetway", dock_cmdr);
XPLMAppendMenuItemWithCommand(os_menu, "Undock Jetway", undock_cmdr);
XPLMAppendMenuItemWithCommand(os_menu, "Toggle UI", toggle_ui_cmdr);
XPLMAppendMenuSeparator(os_menu);
// openSAM -> Remote control
int rc_menu_item = XPLMAppendMenuItem(os_menu, "Remote Control", NULL, 0);
anim_menu = XPLMCreateMenu("Remote Control", os_menu, rc_menu_item, anim_menu_cb, NULL);
XPLMAppendMenuSeparator(os_menu);
XPLMAppendMenuItemWithCommand(os_menu, "Manually activate searching for DGS", activate_cmdr);
XPLMAppendMenuSeparator(os_menu);
// openSAM -> Seasons
int seasons_menu_item = XPLMAppendMenuItem(os_menu, "Seasons", NULL, 0);
seasons_menu = XPLMCreateMenu("Seasons", os_menu, seasons_menu_item, menu_cb, NULL);
auto_item = XPLMAppendMenuItem(seasons_menu, "Automatic", (void *)4, 0);
XPLMAppendMenuSeparator(seasons_menu);
season_item[0] = XPLMAppendMenuItem(seasons_menu, "Winter", (void *)0, 0);
season_item[1] = XPLMAppendMenuItem(seasons_menu, "Spring", (void *)1, 0);
season_item[2] = XPLMAppendMenuItem(seasons_menu, "Summer", (void *)2, 0);
season_item[3] = XPLMAppendMenuItem(seasons_menu, "Autumn", (void *)3, 0);
// ---------------------
set_menu();
// ... and off we go
XPLMRegisterFlightLoopCallback(flight_loop_cb, 2.0, NULL);
}
probe_ref = XPLMCreateProbe(xplm_ProbeY);
if (NULL == probe_ref) {
log_msg("Can't create terrain probe");
goto fail;
}
return 1;
fail:
log_msg("init failure, can't enable openSAM");
init_fail = 1;
return 0;
}
PLUGIN_API void
XPluginReceiveMessage(XPLMPluginID in_from, long in_msg, void *in_param)
{
UNUSED(in_from);
// Everything before XPLM_MSG_AIRPORT_LOADED has bogus datarefs.
// Anyway it's too late for the current scenery.
if ((in_msg == XPLM_MSG_AIRPORT_LOADED) ||
(airport_loaded && (in_msg == XPLM_MSG_SCENERY_LOADED))) {
airport_loaded = 1;
nh = (XPLMGetDatad(plane_lat_dr) >= 0.0);
set_season_auto();
return;
}
// my plane loaded
if (in_msg == XPLM_MSG_PLANE_LOADED && in_param == 0) {
acf_icao.resize(4);
XPLMGetDatab(acf_icao_dr, acf_icao.data(), 0, 4);
for (int i=0; i < 4; i++)
acf_icao[i] = (isupper((uint8_t)acf_icao[i]) || isdigit((uint8_t)acf_icao[i])) ? acf_icao[i] : ' ';
plane_cg_y = F2M * XPLMGetDataf(acf_cg_y_dr);
plane_cg_z = F2M * XPLMGetDataf(acf_cg_z_dr);
float gear_z[2];
if (2 == XPLMGetDatavf(acf_gear_z_dr, gear_z, 0, 2)) { // nose + main wheel
plane_nw_z = -gear_z[0];
plane_mw_z = -gear_z[1];
} else
plane_nw_z = plane_mw_z = plane_cg_z; // fall back to CG
is_helicopter = XPLMGetDatai(is_helicopter_dr);
use_engine_running = 0;
dont_connect_jetway = 0;
log_msg("plane loaded: %s, is_helicopter: %d",
acf_icao.c_str(), is_helicopter);
if (is_helicopter)
return;
// check whether acf is listed in exception files
std::string line; line.reserve(200);
if (find_icao_in_file(acf_icao, base_dir + "acf_use_engine_running.txt", line)) {
use_engine_running = 1;
log_msg("found");
}
if (find_icao_in_file(acf_icao, base_dir + "acf_dont_connect_jetway.txt", line)) {
dont_connect_jetway = 1;
log_msg("found");
}
door_info[0].x = XPLMGetDataf(acf_door_x_dr);
door_info[0].y = XPLMGetDataf(acf_door_y_dr);
door_info[0].z = XPLMGetDataf(acf_door_z_dr);
n_door = 1;
log_msg("plane loaded: %s, plane_cg_y: %1.2f, plane_cg_z: %1.2f, "
"door 1: x: %1.2f, y: %1.2f, z: %1.2f",
acf_icao.c_str(), plane_cg_y, plane_cg_z,
door_info[0].x, door_info[0].y, door_info[0].z);
// check for a second door, seems to be not available by dataref
// data in the acf file is often bogus, so check our own config file first
try {
door_info[1] = door_info_map.at(acf_icao + '2');
n_door++;
log_msg("found door 2 in door_info_map: x: %0.2f, y: %0.2f, z: %0.2f",
door_info[1].x, door_info[1].y, door_info[1].z);
}
catch(const std::out_of_range& ex) {
log_msg("door 2 is not defined in door_info_map");
}
// if nothing found in the config file try the acf
if (n_door == 1) {
char acf_path[512];
char acf_file[256];
XPLMGetNthAircraftModel(XPLM_USER_AIRCRAFT, acf_file, acf_path);
log_msg("acf path: '%s'", acf_path);
FILE *acf = fopen(acf_path, "r");
if (acf) {
char line[200];
int got = 0;
int has_door2 = 0;
// we go the simple brute force way
while (fgets(line, sizeof(line), acf)) {
if (line == strstr(line, "P acf/_has_board_2 ")) {
if (1 != sscanf(line + 19, "%d", &has_door2))
break;
}
if (line == strstr(line, "P acf/_board_2/0 ")) {
if (1 == sscanf(line + 17, "%f", &door_info[1].x)) {
door_info[1].x *= F2M;
got++;
}
}
if (line == strstr(line, "P acf/_board_2/1 ")) {
float y;
if (1 == sscanf(line + 17, "%f", &y)) {
door_info[1].y = y * F2M - plane_cg_y;
got++;
}
}
if (line == strstr(line, "P acf/_board_2/2 ")) {
float z;
if (1 == sscanf(line + 17, "%f", &z)) {
door_info[1].z = z * F2M - plane_cg_z;
got++;
}
}
if (has_door2 && got == 3) {
n_door = 2;
log_msg("found door 2 in acf file: x: %0.2f, y: %0.2f, z: %0.2f",
door_info[1].x, door_info[1].y, door_info[1].z);
break;
}
}
fclose(acf);
}
}
// SAM dgs don't like letters in pos 1-3
if (acf_icao == "A20N")
acf_icao ="A320";
return;
}
// livery loaded
if (in_msg == XPLM_MSG_LIVERY_LOADED && in_param == 0) {
// check ToLiss A321 door config
if (acf_icao != "A321")
return;
log_msg("A321 detected, checking door config");
char path[512];
strcpy(path, xp_dir.c_str());
int len = strlen(path);
int n = XPLMGetDatab(acf_livery_path, path + len, 0, sizeof(path) - len - 50);
path[len + n] = '\0';
strcat(path, "livery.tlscfg");
log_msg("tlscfg path: '%s'", path);
FILE *f = fopen(path, "r");
if (f) {
char line[150];
line[sizeof(line) - 1] = '\0';
while (fgets(line, sizeof(line) - 1, f)) {
if (NULL != strstr(line, "exit_Configuration")) {
if (NULL == strstr(line, "CLASSIC")) {
log_msg("door != CLASSIC, setting n_door to 1");
n_door = 1;
}
break;
}
}
fclose(f);
}
}
}