/
ActorSpawner.cpp
7236 lines (6416 loc) · 276 KB
/
ActorSpawner.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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
Copyright 2013-2020 Petr Ohlidal
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/
/// @file
/// @brief Vehicle spawning logic.
/// @author Petr Ohlidal
/// @date 12/2013
#include "ActorSpawner.h"
#include "AppContext.h"
#include "AirBrake.h"
#include "Airfoil.h"
#include "Application.h"
#include "ApproxMath.h"
#include "AutoPilot.h"
#include "Actor.h"
#include "ActorManager.h"
#include "BitFlags.h"
#include "Buoyance.h"
#include "CacheSystem.h"
#include "CameraManager.h"
#include "CmdKeyInertia.h"
#include "Collisions.h"
#include "DashBoardManager.h"
#include "Differentials.h"
#include "EngineSim.h"
#include "FlexAirfoil.h"
#include "FlexBody.h"
#include "FlexMesh.h"
#include "FlexMeshWheel.h"
#include "FlexObj.h"
#include "GameContext.h"
#include "GfxActor.h"
#include "GfxScene.h"
#include "Console.h"
#include "InputEngine.h"
#include "MeshObject.h"
#include "PointColDetector.h"
#include "Renderdash.h"
#include "ScrewProp.h"
#include "Skidmark.h"
#include "SkinFileFormat.h"
#include "SlideNode.h"
#include "SoundScriptManager.h"
#include "Terrain.h"
#include "TorqueCurve.h"
#include "TurboJet.h"
#include "TurboProp.h"
#include "Utils.h"
#include "VehicleAI.h"
#include <OgreMaterialManager.h>
#include <OgreSceneManager.h>
#include <OgreMovableObject.h>
#include <OgreParticleSystem.h>
#include <OgreEntity.h>
#include <climits>
#include <fmt/format.h>
const char* ACTOR_ID_TOKEN = "@Actor_"; // Appended to material name, followed by actor ID (aka 'trucknum')
using namespace RoR;
/* -------------------------------------------------------------------------- */
// Prepare for loading
/* -------------------------------------------------------------------------- */
void ActorSpawner::ConfigureSections(Ogre::String const & sectionconfig, RigDef::DocumentPtr def)
{
m_selected_modules.push_back(def->root_module);
if (sectionconfig != "")
{
auto result = def->user_modules.find(sectionconfig);
if (result != def->user_modules.end())
{
m_selected_modules.push_back(result->second);
LOG(" == ActorSpawner: Module added to configuration: " + sectionconfig);
}
else
{
this->AddMessage(Message::TYPE_WARNING, "Selected module not found: " + sectionconfig);
}
}
}
void ActorSpawner::CalcMemoryRequirements(ActorMemoryRequirements& req, RigDef::Document::Module* module_def)
{
// 'nodes'
req.num_nodes += module_def->nodes.size();
for (auto& def: module_def->nodes)
{
if (BITMASK_IS_1(def.options, RigDef::Node::OPTION_h_HOOK_POINT))
{
req.num_beams += 1;
}
}
// 'beams'
req.num_beams += module_def->beams.size();
// 'ties'
req.num_beams += module_def->ties.size();
// 'ropes'
req.num_beams += module_def->ropes.size();
// 'hydros'
req.num_beams += module_def->hydros.size();
// 'triggers'
req.num_beams += module_def->triggers.size();
req.num_shocks += module_def->triggers.size();
// 'animators'
req.num_beams += module_def->animators.size();
// 'cinecam'
req.num_nodes += module_def->cinecam.size();
req.num_beams += module_def->cinecam.size() * 8;
// 'shocks', 'shocks2', 'shocks3'
req.num_beams += module_def->shocks.size();
req.num_shocks += module_def->shocks.size();
req.num_beams += module_def->shocks2.size();
req.num_shocks += module_def->shocks2.size();
req.num_beams += module_def->shocks3.size();
req.num_shocks += module_def->shocks3.size();
// 'commands' and 'commands2' (unified)
req.num_beams += module_def->commands2.size();
// 'rotators'
req.num_rotators += module_def->rotators.size();
req.num_rotators += module_def->rotators2.size();
// 'wings'
req.num_wings += module_def->wings.size();
// 'wheels'
for (RigDef::Wheel& wheel: module_def->wheels)
{
req.num_nodes += wheel.num_rays * 2; // BuildWheelObjectAndNodes()
req.num_beams += wheel.num_rays * ((wheel.rigidity_node.IsValidAnyState()) ? 9 : 8); // BuildWheelBeams()
}
// 'wheels2'
for (RigDef::Wheel2& wheel2: module_def->wheels2)
{
req.num_nodes += wheel2.num_rays * 4;
// Rim beams: num_rays*10 (*11 with valid rigidity_node)
// Tyre beams: num_rays*14
req.num_beams += wheel2.num_rays * ((wheel2.rigidity_node.IsValidAnyState()) ? 25 : 24);
}
// 'meshwheels' & 'meshwheels2'
for (RigDef::MeshWheel& meshwheel: module_def->meshwheels)
{
req.num_nodes += meshwheel.num_rays * 2; // BuildWheelObjectAndNodes()
req.num_beams += meshwheel.num_rays * ((meshwheel.rigidity_node.IsValidAnyState()) ? 9 : 8); // BuildWheelBeams()
}
for (RigDef::MeshWheel2& meshwheel2: module_def->meshwheels2)
{
req.num_nodes += meshwheel2.num_rays * 2; // BuildWheelObjectAndNodes()
req.num_beams += meshwheel2.num_rays * ((meshwheel2.rigidity_node.IsValidAnyState()) ? 9 : 8); // BuildWheelBeams()
}
// 'flexbodywheels'
for (RigDef::FlexBodyWheel& flexwheel: module_def->flexbodywheels)
{
req.num_nodes += flexwheel.num_rays * 4;
// Rim beams: num_rays*8
// Tyre beams: num_rays*10 (num_rays*11 with valid rigidity_node)
// Support beams: num_rays*2
req.num_beams += flexwheel.num_rays * ((flexwheel.rigidity_node.IsValidAnyState()) ? 21 : 20);
}
// 'airbrakes'
req.num_airbrakes += module_def->airbrakes.size();
// 'fixes'
req.num_fixes += module_def->fixes.size();
}
void ActorSpawner::InitializeRig()
{
ActorMemoryRequirements & req = m_memory_requirements;
for (auto module: m_selected_modules) // _Root_ module is included
{
this->CalcMemoryRequirements(req, module.get());
}
// Allocate memory as needed
m_actor->ar_beams = new beam_t[req.num_beams];
m_actor->ar_nodes = new node_t[req.num_nodes];
m_actor->ar_nodes_id = new int[req.num_nodes];
for (size_t i = 0; i < req.num_nodes; ++i)
{
m_actor->ar_nodes_id[i] = -1;
}
m_actor->ar_nodes_name = new std::string[req.num_nodes];
if (req.num_shocks > 0)
m_actor->ar_shocks = new shock_t[req.num_shocks];
if (req.num_rotators > 0)
m_actor->ar_rotators = new rotator_t[req.num_rotators];
if (req.num_wings > 0)
m_actor->ar_wings = new wing_t[req.num_wings];
m_actor->ar_minimass.resize(req.num_nodes);
m_actor->exhausts.clear();
memset(m_actor->ar_custom_particles, 0, sizeof(cparticle_t) * MAX_CPARTICLES);
m_actor->ar_num_custom_particles = 0;
memset(m_actor->ar_soundsources, 0, sizeof(soundsource_t) * MAX_SOUNDSCRIPTS_PER_TRUCK);
m_actor->ar_num_soundsources = 0;
memset(m_actor->ar_collcabs, 0, sizeof(int) * MAX_CABS);
memset(m_actor->ar_inter_collcabrate, 0, sizeof(collcab_rate_t) * MAX_CABS);
m_actor->ar_num_collcabs = 0;
memset(m_actor->ar_intra_collcabrate, 0, sizeof(collcab_rate_t) * MAX_CABS);
memset(m_actor->ar_buoycabs, 0, sizeof(int) * MAX_CABS);
m_actor->ar_num_buoycabs = 0;
memset(m_actor->ar_buoycab_types, 0, sizeof(int) * MAX_CABS);
memset(m_actor->m_skid_trails, 0, sizeof(Skidmark *) * (MAX_WHEELS*2));
m_actor->authors.clear();
m_actor->m_odometer_total = 0;
m_actor->m_odometer_user = 0;
m_actor->m_masscount=0;
m_actor->m_disable_smoke = App::gfx_particles_mode->getInt() == 0;
m_actor->m_beam_break_debug_enabled = App::diag_log_beam_break->getBool();
m_actor->m_beam_deform_debug_enabled = App::diag_log_beam_deform->getBool();
m_actor->m_trigger_debug_enabled = App::diag_log_beam_trigger->getBool();
m_actor->ar_origin=Ogre::Vector3::ZERO;
m_actor->m_slidenodes.clear();
m_actor->ar_num_cinecams=0;
m_actor->m_deletion_scene_nodes.clear();
m_actor->ar_state = ActorState::LOCAL_SLEEPING;
m_actor->m_fusealge_airfoil = nullptr;
m_actor->m_fusealge_front = nullptr;
m_actor->m_fusealge_back = nullptr;
m_actor->m_fusealge_width=0;
m_actor->ar_brake_force=30000.0;
m_actor->m_handbrake_force = 2 * m_actor->ar_brake_force;
m_actor->m_num_proped_wheels=0;
m_actor->ar_guisettings_speedo_max_kph=140;
m_actor->ar_num_cameras=0;
for (int i = 0; i < MAX_CAMERAS; ++i)
{
m_actor->ar_camera_node_pos [i] = NODENUM_INVALID;
m_actor->ar_camera_node_dir [i] = NODENUM_INVALID;
m_actor->ar_camera_node_roll[i] = NODENUM_INVALID;
}
#ifdef USE_ANGELSCRIPT
m_actor->ar_vehicle_ai = new VehicleAI(m_actor);
#endif // USE_ANGELSCRIPT
m_actor->ar_airbrake_intensity = 0;
m_actor->alb_minspeed = 0.0f;
m_actor->alb_mode = false;
m_actor->alb_nodash = true;
m_actor->alb_notoggle = false;
m_actor->alb_pulse_time = 2000.0f;
m_actor->alb_pulse_state = false;
m_actor->alb_ratio = 1.0f;
m_actor->alb_timer = 0.0f;
m_actor->ar_anim_shift_timer = 0.0f;
m_actor->cc_mode = false;
m_actor->cc_can_brake = false;
m_actor->cc_target_rpm = 0.0f;
m_actor->cc_target_speed = 0.0f;
m_actor->cc_target_speed_lower_limit = 0.0f;
m_actor->ar_collision_relevant = false;
m_actor->ar_hide_in_actor_list = false;
m_actor->ar_anim_previous_crank = 0.f;
m_actor->sl_enabled = false;
m_actor->sl_speed_limit = 0.f;
m_actor->tc_mode = false;
m_actor->tc_nodash = true;
m_actor->tc_notoggle = false;
m_actor->tc_pulse_time = 2000.0f;
m_actor->tc_pulse_state = false;
m_actor->tc_ratio = 1.f;
m_actor->tc_timer = 0.f;
m_actor->ar_dashboard = new DashBoardManager();
/* Collisions */
if (!App::sim_no_collisions->getBool())
{
m_actor->m_inter_point_col_detector = new PointColDetector(m_actor);
}
if (!App::sim_no_self_collisions->getBool())
{
m_actor->m_intra_point_col_detector = new PointColDetector(m_actor);
}
m_actor->ar_submesh_ground_model = App::GetGameContext()->GetTerrain()->GetCollisions()->defaultgm;
// Lights mode
m_actor->m_flares_mode = App::gfx_flares_mode->getEnum<GfxFlaresMode>();
m_actor->m_definition = m_file;
m_flex_factory = RoR::FlexFactory(this);
m_flex_factory.CheckAndLoadFlexbodyCache();
m_placeholder_managedmat = Ogre::MaterialManager::getSingleton().getByName("rigsofrods/managedmaterial-placeholder"); // Built-in
m_apply_simple_materials = App::diag_simple_materials->getBool();
if (m_apply_simple_materials)
{
m_simple_material_base = Ogre::MaterialManager::getSingleton().getByName("tracks/simple"); // Built-in material
if (m_simple_material_base.isNull())
{
App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_ACTOR, Console::CONSOLE_SYSTEM_WARNING,
"Failed to load built-in material 'tracks/simple'; disabling 'SimpleMaterials'");
m_apply_simple_materials = false;
}
}
m_curr_mirror_prop_type = CustomMaterial::MirrorPropType::MPROP_NONE;
m_curr_mirror_prop_scenenode = nullptr;
}
void ActorSpawner::FinalizeRig()
{
// we should post-process the torque curve if existing
if (m_actor->ar_engine)
{
int result = m_actor->ar_engine->getTorqueCurve()->spaceCurveEvenly(m_actor->ar_engine->getTorqueCurve()->getUsedSpline());
if (result)
{
m_actor->ar_engine->getTorqueCurve()->setTorqueModel("default");
if (result == 1)
{
AddMessage(Message::TYPE_ERROR, "TorqueCurve: Points (rpm) must be in an ascending order. Using default curve");
}
}
//Gearbox
m_actor->ar_engine->SetAutoMode(App::sim_gearbox_mode->getEnum<SimGearboxMode>());
}
// Sanitize trigger_cmdshort and trigger_cmdlong
for (int i=0; i<m_actor->ar_num_beams; i++)
{
shock_t* shock = m_actor->ar_beams[i].shock;
if (shock && ((shock->flags & SHOCK_FLAG_TRG_BLOCKER) || (shock->flags & SHOCK_FLAG_TRG_BLOCKER_A)))
{
shock->trigger_cmdshort = std::min(shock->trigger_cmdshort, m_actor->ar_num_beams - i - 1);
shock->trigger_cmdlong = std::min(shock->trigger_cmdlong , m_actor->ar_num_beams - i - 1);
}
}
//calculate gwps height offset
//get a starting value
m_actor->ar_posnode_spawn_height=m_actor->ar_nodes[0].RelPosition.y;
//start at 0 to avoid a crash whith a 1-node truck
for (int i=0; i<m_actor->ar_num_nodes; i++)
{
// scan and store the y-coord for the lowest node of the truck
if (m_actor->ar_nodes[i].RelPosition.y <= m_actor->ar_posnode_spawn_height)
{
m_actor->ar_posnode_spawn_height = m_actor->ar_nodes[i].RelPosition.y;
}
}
m_actor->ar_main_camera_node_pos = (m_actor->ar_camera_node_pos[0] != NODENUM_INVALID) ? m_actor->ar_camera_node_pos[0] : (NodeNum_t)0;
m_actor->ar_main_camera_node_dir = (m_actor->ar_camera_node_dir[0] != NODENUM_INVALID) ? m_actor->ar_camera_node_dir[0] : (NodeNum_t)0;
m_actor->ar_main_camera_node_roll = (m_actor->ar_camera_node_roll[0]!= NODENUM_INVALID) ? m_actor->ar_camera_node_roll[0] : (NodeNum_t)0;
m_actor->m_has_axles_section = m_actor->m_num_wheel_diffs > 0;
// Calculate mass of each wheel (without rim)
for (int i = 0; i < m_actor->ar_num_wheels; i++)
{
m_actor->ar_wheels[i].wh_mass = 0.0f;
for (int j = 0; j < m_actor->ar_wheels[i].wh_num_nodes; j++)
{
m_actor->ar_wheels[i].wh_mass += m_actor->ar_wheels[i].wh_nodes[j]->mass;
}
}
if (m_actor->m_num_proped_wheels > 0)
{
float proped_wheels_radius_sum = 0.0f;
for (int i = 0; i < m_actor->ar_num_wheels; i++)
{
if (m_actor->ar_wheels[i].wh_propulsed > 0)
{
proped_wheels_radius_sum += m_actor->ar_wheels[i].wh_radius;
}
}
m_actor->m_avg_proped_wheel_radius = proped_wheels_radius_sum / m_actor->m_num_proped_wheels;
}
// Automatically build interwheel differentials from proped wheel pairs
if (m_actor->m_num_wheel_diffs == 0)
{
for (int i = 1; i < m_actor->m_num_proped_wheels; i++)
{
if (i % 2)
{
Differential *diff = new Differential();
diff->di_idx_1 = m_actor->m_proped_wheel_pairs[i - 1];
diff->di_idx_2 = m_actor->m_proped_wheel_pairs[i - 0];
diff->AddDifferentialType(VISCOUS_DIFF);
m_actor->m_wheel_diffs[m_actor->m_num_wheel_diffs] = diff;
m_actor->m_num_wheel_diffs++;
}
}
}
// Automatically build interaxle differentials from interwheel differentials pairs
if (m_actor->m_num_axle_diffs == 0)
{
for (int i = 1; i < m_actor->m_num_wheel_diffs; i++)
{
if (m_actor->m_transfer_case)
{
int a1 = std::min(m_actor->m_transfer_case->tr_ax_1, m_actor->m_transfer_case->tr_ax_2);
int a2 = std::max(m_actor->m_transfer_case->tr_ax_1, m_actor->m_transfer_case->tr_ax_2);
if ((a1 == i - 1) && (a2 == i - 0))
continue;
}
Differential *diff = new Differential();
diff->di_idx_1 = i - 1;
diff->di_idx_2 = i - 0;
if (m_actor->m_has_axles_section)
diff->AddDifferentialType(LOCKED_DIFF);
else
diff->AddDifferentialType(VISCOUS_DIFF);
m_actor->m_axle_diffs[m_actor->m_num_axle_diffs] = diff;
m_actor->m_num_axle_diffs++;
}
}
// Automatically build an additional interaxle differential for the transfer case
if (m_actor->m_transfer_case && m_actor->m_transfer_case->tr_ax_2 >= 0)
{
Differential *diff = new Differential();
diff->di_idx_1 = m_actor->m_transfer_case->tr_ax_1;
diff->di_idx_2 = m_actor->m_transfer_case->tr_ax_2;
diff->AddDifferentialType(LOCKED_DIFF);
m_actor->m_axle_diffs[m_actor->m_num_axle_diffs] = diff;
}
if (m_actor->ar_main_camera_node_dir == 0 || m_actor->ar_main_camera_node_dir == NODENUM_INVALID)
{
Ogre::Vector3 ref = m_actor->ar_nodes[m_actor->ar_main_camera_node_pos].RelPosition;
// Step 1: Find a suitable camera node dir
float max_dist = 0.0f;
NodeNum_t furthest_node = 0;
for (int i = 0; i < m_actor->ar_num_nodes; i++)
{
float dist = m_actor->ar_nodes[i].RelPosition.squaredDistance(ref);
if (dist > max_dist)
{
max_dist = dist;
furthest_node = (NodeNum_t)i;
}
}
m_actor->ar_main_camera_node_dir = furthest_node;
// Step 2: Correct the misalignment
Ogre::Vector3 dir = m_actor->ar_nodes[furthest_node].RelPosition - ref;
float offset = atan2(dir.dotProduct(Ogre::Vector3::UNIT_Z), dir.dotProduct(Ogre::Vector3::UNIT_X));
m_actor->ar_main_camera_dir_corr = Ogre::Quaternion(Ogre::Radian(offset), Ogre::Vector3::UNIT_Y);
}
if (m_actor->ar_camera_node_pos[0] != NODENUM_INVALID)
{
// store the y-difference between the trucks lowest node and the campos-node for the gwps system
m_actor->ar_posnode_spawn_height = m_actor->ar_nodes[m_actor->ar_camera_node_pos[0]].RelPosition.y - m_actor->ar_posnode_spawn_height;
}
else
{
//this can not be an airplane, just set it to 0.
m_actor->ar_posnode_spawn_height = 0.0f;
}
//cameras workaround
for (int i=0; i<m_actor->ar_num_cameras; i++)
{
Ogre::Vector3 dir_node_offset = m_actor->ar_nodes[m_actor->ar_camera_node_dir[i]].RelPosition - m_actor->ar_nodes[m_actor->ar_camera_node_pos[i]].RelPosition;
Ogre::Vector3 roll_node_offset = m_actor->ar_nodes[m_actor->ar_camera_node_roll[i]].RelPosition - m_actor->ar_nodes[m_actor->ar_camera_node_pos[i]].RelPosition;
Ogre::Vector3 cross = dir_node_offset.crossProduct(roll_node_offset);
m_actor->ar_camera_node_roll_inv[i]=cross.y > 0;
if (m_actor->ar_camera_node_roll_inv[i])
{
AddMessage(Message::TYPE_WARNING, "camera definition is probably invalid and has been corrected. It should be center, back, left");
}
}
//wing closure
if (m_first_wing_index!=-1)
{
if (m_actor->ar_autopilot != nullptr)
{
m_actor->ar_autopilot->setInertialReferences(
& m_actor->ar_nodes[m_airplane_left_light],
& m_actor->ar_nodes[m_airplane_right_light],
m_actor->m_fusealge_back,
& m_actor->ar_nodes[m_actor->ar_camera_node_pos[0]]
);
}
//inform wing segments
float span=m_actor->ar_nodes[m_actor->ar_wings[m_first_wing_index].fa->nfrd].RelPosition.distance(m_actor->ar_nodes[m_actor->ar_wings[m_actor->ar_num_wings-1].fa->nfld].RelPosition);
m_actor->ar_wings[m_first_wing_index].fa->enableInducedDrag(span,m_wing_area, false);
m_actor->ar_wings[m_actor->ar_num_wings-1].fa->enableInducedDrag(span,m_wing_area, true);
//wash calculator
WashCalculator();
}
this->UpdateCollcabContacterNodes();
m_flex_factory.SaveFlexbodiesToCache();
m_actor->GetGfxActor()->SortFlexbodies();
}
/* -------------------------------------------------------------------------- */
// Processing functions and utilities.
/* -------------------------------------------------------------------------- */
void ActorSpawner::WashCalculator()
{
//we will compute wash
int w,p;
for (p=0; p<m_actor->ar_num_aeroengines; p++)
{
Ogre::Vector3 prop=m_actor->ar_nodes[m_actor->ar_aeroengines[p]->getNoderef()].RelPosition;
float radius=m_actor->ar_aeroengines[p]->getRadius();
for (w=0; w<m_actor->ar_num_wings; w++)
{
//left wash
Ogre::Vector3 wcent=((m_actor->ar_nodes[m_actor->ar_wings[w].fa->nfld].RelPosition+m_actor->ar_nodes[m_actor->ar_wings[w].fa->nfrd].RelPosition)/2.0);
//check if wing is near enough along X (less than 15m back)
if (wcent.x>prop.x && wcent.x<prop.x+15.0)
{
//check if it's okay vertically
if (wcent.y>prop.y-radius && wcent.y<prop.y+radius)
{
//okay, compute wash coverage ratio along Z
float wleft=(m_actor->ar_nodes[m_actor->ar_wings[w].fa->nfld].RelPosition).z;
float wright=(m_actor->ar_nodes[m_actor->ar_wings[w].fa->nfrd].RelPosition).z;
float pleft=prop.z+radius;
float pright=prop.z-radius;
float aleft=wleft;
if (pleft<aleft) aleft=pleft;
float aright=wright;
if (pright>aright) aright=pright;
if (aright<aleft)
{
//we have a wash
float wratio=(aleft-aright)/(wleft-wright);
m_actor->ar_wings[w].fa->addwash(p, wratio);
Ogre::String msg = "Wing "+TOSTRING(w)+" is washed by prop "+TOSTRING(p)+" at "+TOSTRING((float)(wratio*100.0))+"%";
AddMessage(Message::TYPE_INFO, msg);
}
}
}
}
}
}
void ActorSpawner::ProcessTurbojet(RigDef::Turbojet & def)
{
NodeNum_t front,back,ref;
front = GetNodeIndexOrThrow(def.front_node);
back = GetNodeIndexOrThrow(def.back_node);
ref = GetNodeIndexOrThrow(def.side_node);
Turbojet *tj = new Turbojet(m_actor, front, back, ref, def);
// Visuals
std::string nozzle_name = this->ComposeName("TurbojetNozzle", m_actor->ar_num_aeroengines);
Ogre::Entity* nozzle_ent = App::GetGfxScene()->GetSceneManager()->createEntity(nozzle_name, "nozzle.mesh", m_custom_resource_group);
this->SetupNewEntity(nozzle_ent, Ogre::ColourValue(1, 0.5, 0.5));
Ogre::Entity* afterburn_ent = nullptr;
if (def.wet_thrust > 0.f)
{
std::string flame_name = this->ComposeName("AfterburnerFlame", m_actor->ar_num_aeroengines);
afterburn_ent = App::GetGfxScene()->GetSceneManager()->createEntity(flame_name, "abflame.mesh", m_custom_resource_group);
this->SetupNewEntity(afterburn_ent, Ogre::ColourValue(1, 1, 0));
}
std::string propname = this->ComposeName("Turbojet", m_actor->ar_num_aeroengines);
tj->tjet_visual.SetNodes(front, back, ref);
tj->tjet_visual.SetupVisuals(def, m_actor->ar_num_aeroengines,
propname, nozzle_ent, afterburn_ent);
if (!m_actor->m_disable_smoke)
{
tj->tjet_visual.SetVisible(true);
}
m_actor->ar_aeroengines[m_actor->ar_num_aeroengines]=tj;
m_actor->ar_driveable=AIRPLANE;
if (m_actor->ar_autopilot == nullptr && m_actor->ar_state != ActorState::NETWORKED_OK)
{
m_actor->ar_autopilot=new Autopilot(m_actor->ar_instance_id);
}
m_actor->ar_num_aeroengines++;
}
std::string ActorSpawner::ComposeName(const char* type, int number)
{
char buf[500];
snprintf(buf, 500, "%s_%d%s%d", type, number, ACTOR_ID_TOKEN, m_actor->ar_instance_id);
return buf;
}
void ActorSpawner::ProcessScrewprop(RigDef::Screwprop & def)
{
if (! CheckScrewpropLimit(1))
{
return;
}
m_actor->ar_screwprops[m_actor->ar_num_screwprops] = new Screwprop(
m_actor,
GetNodeIndexOrThrow(def.prop_node),
GetNodeIndexOrThrow(def.back_node),
GetNodeIndexOrThrow(def.top_node),
def.power
);
m_actor->ar_driveable=BOAT;
m_actor->ar_num_screwprops++;
}
void ActorSpawner::ProcessFusedrag(RigDef::Fusedrag & def)
{
//parse fusedrag
NodeNum_t front_node_idx = GetNodeIndexOrThrow(def.front_node);
float width = 1.f;
float factor = 1.f;
char fusefoil[256];
strncpy(fusefoil, def.airfoil_name.c_str(), 255);
if (def.autocalc)
{
// fusedrag autocalculation
// calculate fusedrag by truck size
factor = def.area_coefficient;
width = (m_fuse_z_max - m_fuse_z_min) * (m_fuse_y_max - m_fuse_y_min) * factor;
m_actor->m_fusealge_airfoil = new Airfoil(fusefoil);
m_actor->m_fusealge_front = & m_actor->ar_nodes[front_node_idx];
m_actor->m_fusealge_back = & m_actor->ar_nodes[front_node_idx]; // This equals v0.38 / v0.4.0.7, but it's probably a bug
m_actor->m_fusealge_width = width;
AddMessage(Message::TYPE_INFO, "Fusedrag autocalculation size: "+TOSTRING(width)+" m^2");
}
else
{
// original fusedrag calculation
width = def.approximate_width;
m_actor->m_fusealge_airfoil = new Airfoil(fusefoil);
m_actor->m_fusealge_front = & m_actor->ar_nodes[front_node_idx];
m_actor->m_fusealge_back = & m_actor->ar_nodes[front_node_idx]; // This equals v0.38 / v0.4.0.7, but it's probably a bug
m_actor->m_fusealge_width = width;
}
}
void ActorSpawner::BuildAeroEngine(
NodeNum_t ref_node_index,
NodeNum_t back_node_index,
NodeNum_t blade_1_node_index,
NodeNum_t blade_2_node_index,
NodeNum_t blade_3_node_index,
NodeNum_t blade_4_node_index,
NodeNum_t couplenode_index,
bool is_turboprops,
Ogre::String const & airfoil,
float power,
float pitch
)
{
int aeroengine_index = m_actor->ar_num_aeroengines;
Turboprop *turbo_prop = new Turboprop(
m_actor,
this->ComposeName("Turboprop", aeroengine_index).c_str(),
ref_node_index,
back_node_index,
blade_1_node_index,
blade_2_node_index,
blade_3_node_index,
blade_4_node_index,
couplenode_index,
power,
airfoil,
m_actor->m_disable_smoke,
! is_turboprops,
pitch
);
m_actor->ar_aeroengines[m_actor->ar_num_aeroengines] = turbo_prop;
m_actor->ar_num_aeroengines++;
m_actor->ar_driveable = AIRPLANE;
/* Autopilot */
if (m_actor->ar_autopilot == nullptr && m_actor->ar_state != ActorState::NETWORKED_OK)
{
m_actor->ar_autopilot = new Autopilot(m_actor->ar_instance_id);
}
/* Visuals */
float scale = m_actor->ar_nodes[ref_node_index].RelPosition.distance(m_actor->ar_nodes[blade_1_node_index].RelPosition) / 2.25f;
for (RoR::Prop& prop: m_actor->m_gfx_actor->m_props)
{
if ((prop.pp_node_ref == ref_node_index) && (prop.pp_aero_propeller_blade || prop.pp_aero_propeller_spin))
{
prop.pp_scene_node->scale(scale, scale, scale);
prop.pp_aero_engine_idx = aeroengine_index;
}
}
}
void ActorSpawner::ProcessTurboprop2(RigDef::Turboprop2 & def)
{
const NodeNum_t p3_node_index = (def.blade_tip_nodes[2].IsValidAnyState()) ? GetNodeIndexOrThrow(def.blade_tip_nodes[2]) : -1;
const NodeNum_t p4_node_index = (def.blade_tip_nodes[3].IsValidAnyState()) ? GetNodeIndexOrThrow(def.blade_tip_nodes[3]) : -1;
const NodeNum_t couple_node_index = (def.couple_node.IsValidAnyState()) ? GetNodeIndexOrThrow(def.couple_node) : -1;
BuildAeroEngine(
GetNodeIndexOrThrow(def.reference_node),
GetNodeIndexOrThrow(def.axis_node),
GetNodeIndexOrThrow(def.blade_tip_nodes[0]),
GetNodeIndexOrThrow(def.blade_tip_nodes[1]),
p3_node_index,
p4_node_index,
couple_node_index,
true,
def.airfoil,
def.turbine_power_kW,
-10
);
}
void ActorSpawner::ProcessDescription(Ogre::String const& line)
{
m_actor->m_description.push_back(line);
}
void ActorSpawner::ProcessPistonprop(RigDef::Pistonprop & def)
{
const NodeNum_t p3_node_index = (def.blade_tip_nodes[2].IsValidAnyState()) ? GetNodeIndexOrThrow(def.blade_tip_nodes[2]) : -1;
const NodeNum_t p4_node_index = (def.blade_tip_nodes[3].IsValidAnyState()) ? GetNodeIndexOrThrow(def.blade_tip_nodes[3]) : -1;
const NodeNum_t couple_node_index = (def.couple_node.IsValidAnyState()) ? GetNodeIndexOrThrow(def.couple_node) : -1;
BuildAeroEngine(
GetNodeIndexOrThrow(def.reference_node),
GetNodeIndexOrThrow(def.axis_node),
GetNodeIndexOrThrow(def.blade_tip_nodes[0]),
GetNodeIndexOrThrow(def.blade_tip_nodes[1]),
p3_node_index,
p4_node_index,
couple_node_index,
false,
def.airfoil,
def.turbine_power_kW,
def.pitch
);
}
void ActorSpawner::ProcessAirbrake(RigDef::Airbrake & def)
{
const int airbrake_idx = static_cast<int>(m_actor->ar_airbrakes.size());
Airbrake* ab = new Airbrake(
m_actor,
this->ComposeName("Airbrake", airbrake_idx).c_str(),
airbrake_idx,
GetNodePointerOrThrow(def.reference_node),
GetNodePointerOrThrow(def.x_axis_node),
GetNodePointerOrThrow(def.y_axis_node),
GetNodePointerOrThrow(def.aditional_node),
def.offset,
def.width,
def.height,
def.max_inclination_angle,
m_cab_material_name,
def.texcoord_x1,
def.texcoord_y1,
def.texcoord_x2,
def.texcoord_y2,
def.lift_coefficient
);
m_actor->ar_airbrakes.push_back(ab);
AirbrakeGfx abx;
// entity
abx.abx_entity = ab->ec;
ab->ec = nullptr;
// mesh
abx.abx_mesh = ab->msh;
ab->msh.setNull();
// scenenode
abx.abx_scenenode = ab->snode;
ab->snode = nullptr;
// offset
abx.abx_offset = ab->offset;
ab->offset = Ogre::Vector3::ZERO;
// Nodes - just copy
abx.abx_ref_node = ab->noderef->pos;
abx.abx_x_node = ab->nodex->pos;
abx.abx_y_node = ab->nodey->pos;
m_actor->m_gfx_actor->m_gfx_airbrakes.push_back(abx);
}
void ActorSpawner::ProcessWing(RigDef::Wing & def)
{
if ((m_first_wing_index != -1) && (m_actor->ar_wings[m_actor->ar_num_wings - 1].fa == nullptr))
{
this->AddMessage(Message::TYPE_ERROR, "Unable to process wing, previous wing has no Airfoil");
return;
}
if (def.airfoil == "") // May happen for malformed `wings` entry in truckfile
{
this->AddMessage(Message::TYPE_ERROR, "Unable to process wing, no Airfoil defined");
return;
}
m_actor->GetGfxActor()->UpdateSimDataBuffer(); // fill all current nodes - needed to setup flexing meshes
NodeNum_t node1 = this->GetNodeIndexOrThrow(def.nodes[1]);
const std::string wing_name = this->ComposeName("Wing", m_actor->ar_num_wings);
auto flex_airfoil = new FlexAirfoil(
wing_name,
m_actor,
this->GetNodeIndexOrThrow(def.nodes[0]),
node1,
this->GetNodeIndexOrThrow(def.nodes[2]),
this->GetNodeIndexOrThrow(def.nodes[3]),
this->GetNodeIndexOrThrow(def.nodes[4]),
this->GetNodeIndexOrThrow(def.nodes[5]),
this->GetNodeIndexOrThrow(def.nodes[6]),
this->GetNodeIndexOrThrow(def.nodes[7]),
m_cab_material_name,
Ogre::Vector2(def.tex_coords[0], def.tex_coords[1]),
Ogre::Vector2(def.tex_coords[2], def.tex_coords[3]),
Ogre::Vector2(def.tex_coords[4], def.tex_coords[5]),
Ogre::Vector2(def.tex_coords[6], def.tex_coords[7]),
(char)def.control_surface,
def.chord_point,
def.min_deflection,
def.max_deflection,
def.airfoil,
def.efficacy_coef,
m_actor->ar_state != ActorState::NETWORKED_OK
);
Ogre::Entity* entity = nullptr;
try
{
const std::string wing_instance_name = this->ComposeName("WingEntity", m_actor->ar_num_wings);
entity = App::GetGfxScene()->GetSceneManager()->createEntity(wing_instance_name, wing_name);
m_actor->m_deletion_entities.emplace_back(entity);
this->SetupNewEntity(entity, Ogre::ColourValue(0.5, 1, 0));
}
catch (...)
{
this->AddMessage(Message::TYPE_ERROR, std::string("Failed to load mesh (flexbody wing): ") + wing_name);
delete flex_airfoil;
return;
}
// induced drag
if (m_first_wing_index == -1)
{
m_first_wing_index = m_actor->ar_num_wings;
m_wing_area=ComputeWingArea(
m_actor->ar_nodes[flex_airfoil->nfld].AbsPosition, m_actor->ar_nodes[flex_airfoil->nfrd].AbsPosition,
m_actor->ar_nodes[flex_airfoil->nbld].AbsPosition, m_actor->ar_nodes[flex_airfoil->nbrd].AbsPosition
);
}
else
{
wing_t & previous_wing = m_actor->ar_wings[m_actor->ar_num_wings - 1];
if (node1 != previous_wing.fa->nfld)
{
wing_t & start_wing = m_actor->ar_wings[m_first_wing_index];
//discontinuity
//inform wing segments
float span = m_actor->ar_nodes[start_wing.fa->nfrd].RelPosition.distance(m_actor->ar_nodes[previous_wing.fa->nfld].RelPosition );
start_wing.fa->enableInducedDrag(span, m_wing_area, false);
previous_wing.fa->enableInducedDrag(span, m_wing_area, true);
//we want also to add positional lights for first wing
if (m_generate_wing_position_lights && (m_actor->m_flares_mode != GfxFlaresMode::NONE))
{
//Left green
m_airplane_left_light=previous_wing.fa->nfld;
RoR::Prop left_green_prop;
left_green_prop.pp_node_ref=previous_wing.fa->nfld;
left_green_prop.pp_node_x=previous_wing.fa->nflu;
left_green_prop.pp_node_y=previous_wing.fa->nfld; //ignored
left_green_prop.pp_offset.x=0.5;
left_green_prop.pp_offset.y=0.0;
left_green_prop.pp_offset.z=0.0;
left_green_prop.pp_beacon_rot_angle[0]=0.0;
left_green_prop.pp_beacon_rot_rate[0]=1.0;
left_green_prop.pp_beacon_type='L';
left_green_prop.pp_beacon_light[0]=nullptr; //no light
//the flare billboard
left_green_prop.pp_beacon_scene_node[0] = App::GetGfxScene()->GetSceneManager()->getRootSceneNode()->createChildSceneNode();
left_green_prop.pp_beacon_bbs[0]=App::GetGfxScene()->GetSceneManager()->createBillboardSet(this->ComposeName("Prop", static_cast<int>(m_actor->m_gfx_actor->m_props.size())+1),1);
left_green_prop.pp_beacon_bbs[0]->createBillboard(0,0,0);
if (left_green_prop.pp_beacon_bbs[0])
{
left_green_prop.pp_beacon_bbs[0]->setVisibilityFlags(DEPTHMAP_DISABLED);
left_green_prop.pp_beacon_bbs[0]->setMaterialName("tracks/greenflare");
left_green_prop.pp_beacon_scene_node[0]->attachObject(left_green_prop.pp_beacon_bbs[0]);
}
left_green_prop.pp_beacon_scene_node[0]->setVisible(false);
left_green_prop.pp_beacon_bbs[0]->setDefaultDimensions(0.5, 0.5);
m_actor->m_gfx_actor->m_props.push_back(left_green_prop);
//Left flash
RoR::Prop left_flash_prop;
left_flash_prop.pp_node_ref=previous_wing.fa->nbld;
left_flash_prop.pp_node_x=previous_wing.fa->nblu;
left_flash_prop.pp_node_y=previous_wing.fa->nbld; //ignored
left_flash_prop.pp_offset.x=0.5;
left_flash_prop.pp_offset.y=0.0;
left_flash_prop.pp_offset.z=0.0;
left_flash_prop.pp_beacon_rot_angle[0]=0.5; //alt
left_flash_prop.pp_beacon_rot_rate[0]=1.0;
left_flash_prop.pp_beacon_type='w';
//light
std::string prop_name = this->ComposeName("Prop", static_cast<int>(m_actor->m_gfx_actor->m_props.size())+1);
left_flash_prop.pp_beacon_light[0]=App::GetGfxScene()->GetSceneManager()->createLight(prop_name);
left_flash_prop.pp_beacon_light[0]->setType(Ogre::Light::LT_POINT);
left_flash_prop.pp_beacon_light[0]->setDiffuseColour( Ogre::ColourValue(1.0, 1.0, 1.0));
left_flash_prop.pp_beacon_light[0]->setSpecularColour( Ogre::ColourValue(1.0, 1.0, 1.0));
left_flash_prop.pp_beacon_light[0]->setAttenuation(50.0, 1.0, 0.3, 0.0);
left_flash_prop.pp_beacon_light[0]->setCastShadows(false);
left_flash_prop.pp_beacon_light[0]->setVisible(false);
//the flare billboard
left_flash_prop.pp_beacon_scene_node[0] = App::GetGfxScene()->GetSceneManager()->getRootSceneNode()->createChildSceneNode();
left_flash_prop.pp_beacon_bbs[0]=App::GetGfxScene()->GetSceneManager()->createBillboardSet(prop_name,1);