forked from RigsOfRods/rigs-of-rods
/
Beam.cpp
6409 lines (5649 loc) · 189 KB
/
Beam.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
For more information, see http://www.rigsofrods.com/
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/>.
*/
#include "Beam.h"
#include <Ogre.h>
#ifdef ROR_USE_OGRE_1_9
# include <Overlay/OgreOverlayManager.h>
# include <Overlay/OgreOverlay.h>
#else
# include <OgreOverlayManager.h>
# include <OgreOverlayElement.h>
#endif
#include "AirBrake.h"
#include "Airfoil.h"
#include "Application.h"
#include "ApproxMath.h"
#include "AutoPilot.h"
#include "BeamData.h"
#include "BeamEngine.h"
#include "BeamFactory.h"
#include "BeamStats.h"
#include "Buoyance.h"
#include "CacheSystem.h"
#include "CameraManager.h"
#include "CartesianToTriangleTransform.h"
#include "CmdKeyInertia.h"
#include "Collisions.h"
#include "Console.h"
#include "DashBoardManager.h"
#include "Differentials.h"
#include "DynamicCollisions.h"
#include "ErrorUtils.h"
#include "FlexAirfoil.h"
#include "FlexBody.h"
#include "FlexMesh.h"
#include "FlexMeshWheel.h"
#include "FlexObj.h"
#include "GuiManagerInterface.h"
#include "IHeightFinder.h"
#include "InputEngine.h"
#include "Language.h"
#include "MaterialReplacer.h"
#include "MeshObject.h"
#include "MovableText.h"
#include "Network.h"
#include "PointColDetector.h"
#include "PositionStorage.h"
#include "Replay.h"
#include "RigLoadingProfiler.h"
#include "RigSpawner.h"
#include "ScrewProp.h"
#include "Scripting.h"
#include "Settings.h"
#include "Skidmark.h"
#include "SlideNode.h"
#include "SoundScriptManager.h"
#include "TerrainManager.h"
#include "ThreadPool.h"
#include "Triangle.h"
#include "TurboJet.h"
#include "TurboProp.h"
#include "VideoCamera.h"
#include "Water.h"
#include "GUIManager.h"
// DEBUG UTILITY
//#include "d:\Projects\Git\rigs-of-rods\tools\rig_inspector\RoR_RigInspector.h"
#define LOAD_RIG_PROFILE_CHECKPOINT(ENTRY) rig_loading_profiler->Checkpoint(RoR::RigLoadingProfiler::ENTRY);
#include "RigDef_Parser.h"
#include "RigDef_Validator.h"
// some gcc fixes
#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
#pragma GCC diagnostic ignored "-Wfloat-equal"
#endif //OGRE_PLATFORM_LINUX
using namespace Ogre;
using namespace RoR;
Beam::~Beam()
{
if (gEnv->network && state != NETWORKED)
{
NetworkStreamManager::getSingleton().removeStream(this->getSourceID(), this->getStreamID());
}
// TODO: IMPROVE below: delete/destroy prop entities, etc
deleting = true;
state = DELETED;
// hide everything, prevents deleting stuff while drawing
this->setBeamVisibility(false);
this->setMeshVisibility(false);
// delete all classes we might have constructed
#ifdef USE_MYGUI
if (dash) delete dash; dash=0;
#endif // USE_MYGUI
// stop all the Sounds
#ifdef USE_OPENAL
for (int i=SS_TRIG_NONE+1; i<SS_MAX_TRIG; i++)
{
SoundScriptManager::getSingleton().trigStop(this->trucknum, i);
}
StopAllSounds();
#endif // USE_OPENAL
// destruct and remove every tiny bit of stuff we created :-|
if (engine) delete engine; engine=0;
if (buoyance) delete buoyance; buoyance=0;
if (autopilot) delete autopilot; autopilot=0;
if (fuseAirfoil) delete fuseAirfoil; fuseAirfoil=0;
if (cabMesh) delete cabMesh; cabMesh=0;
if (materialFunctionMapper) delete materialFunctionMapper; materialFunctionMapper=0;
if (replay) delete replay; replay=0;
// TODO: Make sure we catch everything here
// remove all scene nodes
if (deletion_sceneNodes.size() > 0)
{
for (unsigned int i=0; i<deletion_sceneNodes.size(); i++)
{
if (!deletion_sceneNodes[i]) continue;
deletion_sceneNodes[i]->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(deletion_sceneNodes[i]);
}
deletion_sceneNodes.clear();
}
// remove all entities
if (deletion_Entities.size() > 0)
{
for (unsigned int i=0; i<deletion_Entities.size(); i++)
{
if (!deletion_Entities[i]) continue;
deletion_Entities[i]->detachAllObjectsFromBone();
gEnv->sceneManager->destroyEntity(deletion_Entities[i]->getName());
}
deletion_Entities.clear();
}
// delete skidmarks as well?!
// delete wings
for (int i=0; i<free_wing;i++)
{
// flexAirfoil, airfoil
if (wings[i].fa) delete wings[i].fa;
if (wings[i].cnode)
{
wings[i].cnode->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(wings[i].cnode);
}
}
// delete aeroengines
for (int i=0; i<free_aeroengine;i++)
{
if (aeroengines[i]) delete aeroengines[i];
}
// delete screwprops
for (int i=0; i<free_screwprop;i++)
{
if (screwprops[i]) delete screwprops[i];
}
// delete airbrakes
for (int i=0; i<free_airbrake;i++)
{
if (airbrakes[i]) delete airbrakes[i];
}
// delete flexbodies
for (int i=0; i<free_flexbody;i++)
{
if (flexbodies[i]) delete flexbodies[i];
}
// delete meshwheels
for (int i=0; i<free_wheel;i++)
{
if (vwheels[i].fm) delete vwheels[i].fm;
if (vwheels[i].cnode)
{
vwheels[i].cnode->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(vwheels[i].cnode);
}
}
// delete cablight
if (cablight) gEnv->sceneManager->destroyLight(cablight);
// delete props
for (int i=0; i<free_prop;i++)
{
for (int k = 0; k < 4; ++k)
{
if (props[i].beacon_flare_billboard_scene_node[k])
{
Ogre::SceneNode* scene_node = props[i].beacon_flare_billboard_scene_node[k];
scene_node->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(scene_node);
}
if (props[i].beacon_light[k])
{
gEnv->sceneManager->destroyLight(props[i].beacon_light[k]);
}
}
if (props[i].scene_node)
{
props[i].scene_node->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(props[i].scene_node);
}
if (props[i].wheel)
{
props[i].wheel->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(props[i].wheel);
}
if (props[i].mo)
{
delete props[i].mo;
}
if (props[i].wheelmo)
{
delete props[i].wheelmo;
}
}
// delete flares
for (int i=0; i<free_flare; i++)
{
if (flares[i].snode)
{
flares[i].snode->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(flares[i].snode);
}
if (flares[i].bbs) gEnv->sceneManager->destroyBillboardSet(flares[i].bbs);
if (flares[i].light) gEnv->sceneManager->destroyLight(flares[i].light);
}
// delete exhausts
for (std::vector < exhaust_t >::iterator it=exhausts.begin(); it!=exhausts.end(); it++)
{
if (it->smokeNode)
{
it->smokeNode->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(it->smokeNode);
}
if (it->smoker)
{
it->smoker->removeAllAffectors();
it->smoker->removeAllEmitters();
gEnv->sceneManager->destroyParticleSystem(it->smoker);
}
}
// delete cparticles
for (int i=0; i<free_cparticle; i++)
{
if (cparticles[i].snode)
{
cparticles[i].snode->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(cparticles[i].snode);
}
if (cparticles[i].psys)
{
cparticles[i].psys->removeAllAffectors();
cparticles[i].psys->removeAllEmitters();
gEnv->sceneManager->destroyParticleSystem(cparticles[i].psys);
}
}
// delete beams
for (int i=0; i<free_beam; i++)
{
if (beams[i].mSceneNode)
{
beams[i].mSceneNode->removeAndDestroyAllChildren();
gEnv->sceneManager->destroySceneNode(beams[i].mSceneNode);
}
}
// delete Rails
for (std::vector< RailGroup* >::iterator it = mRailGroups.begin(); it != mRailGroups.end(); it++)
{
// signal to the Rail that
(*it)->cleanUp();
delete (*it);
}
if (netMT)
{
netMT->setVisible(false);
delete netMT;
netMT = 0;
}
for (VideoCamera* v : vidcams)
{
delete v;
}
if (materialReplacer) delete materialReplacer;
if (intraPointCD) delete intraPointCD;
if (interPointCD) delete interPointCD;
if (cmdInertia) delete cmdInertia;
if (hydroInertia) delete hydroInertia;
if (rotaInertia) delete rotaInertia;
for (int i = 0; i < free_axle; ++i)
{
if (axles[i] != nullptr) delete (axles[i]);
}
}
// This method scales trucks. Stresses should *NOT* be scaled, they describe
// the material type and they do not depend on length or scale.
void Beam::scaleTruck(float value)
{
BES_GFX_START(BES_GFX_ScaleTruck);
if (value<0) return;
currentScale *= value;
// scale beams
for (int i=0;i<free_beam;i++)
{
//beams[i].k *= value;
beams[i].d *= value;
beams[i].L *= value;
beams[i].refL *= value;
beams[i].Lhydro *= value;
beams[i].hydroRatio *= value;
beams[i].diameter *= value;
}
// scale nodes
Vector3 refpos = nodes[0].AbsPosition;
Vector3 relpos = nodes[0].RelPosition;
Vector3 smopos = nodes[0].smoothpos;
for (int i=1;i<free_node;i++)
{
initial_node_pos[i] = refpos + (initial_node_pos[i]-refpos) * value;
nodes[i].AbsPosition = refpos + (nodes[i].AbsPosition-refpos) * value;
nodes[i].RelPosition = relpos + (nodes[i].RelPosition-relpos) * value;
nodes[i].smoothpos = smopos + (nodes[i].smoothpos-smopos) * value;
nodes[i].Velocity *= value;
nodes[i].Forces *= value;
nodes[i].mass *= value;
}
updateSlideNodePositions();
// props and stuff
// TOFIX: care about prop positions as well!
for (int i=0;i<free_prop;i++)
{
if (props[i].scene_node)
props[i].scene_node->scale(value, value, value);
if (props[i].wheel)
props[i].wheel->scale(value, value, value);
if (props[i].wheel)
props[i].wheelpos = relpos + (props[i].wheelpos-relpos) * value;
if (props[i].beacon_flare_billboard_scene_node[0])
props[i].beacon_flare_billboard_scene_node[0]->scale(value, value, value);
if (props[i].beacon_flare_billboard_scene_node[1])
props[i].beacon_flare_billboard_scene_node[1]->scale(value, value, value);
if (props[i].beacon_flare_billboard_scene_node[2])
props[i].beacon_flare_billboard_scene_node[2]->scale(value, value, value);
if (props[i].beacon_flare_billboard_scene_node[3])
props[i].beacon_flare_billboard_scene_node[3]->scale(value, value, value);
}
// tell the cabmesh that resizing is ok, and they dont need to break ;)
if (cabMesh) cabMesh->scale(value);
// update engine values
if (engine)
{
//engine->maxRPM *= value;
//engine->iddleRPM *= value;
engine->engineTorque *= value;
//engine->stallRPM *= value;
//engine->brakingTorque *= value;
}
// todo: scale flexbody
for (int i=0;i<free_flexbody;i++)
{
flexbodies[i]->getSceneNode()->scale(value, value, value);
}
// todo: fix meshwheels
//for (int i=0;i<free_wheel;i++)
//{
//if (vwheels[i].cnode) vwheels[i].cnode->scale(value, value, value);
//if (vwheels[i].fm && vwheels[i].cnode) vwheels[i].cnode->scale(value, value, value);
//}
BES_GFX_STOP(BES_GFX_ScaleTruck);
}
void Beam::initSimpleSkeleton()
{
simpleSkeletonManualObject = gEnv->sceneManager->createManualObject();
simpleSkeletonManualObject->estimateIndexCount(free_beam*2);
simpleSkeletonManualObject->setCastShadows(false);
simpleSkeletonManualObject->setDynamic(true);
simpleSkeletonManualObject->setRenderingDistance(300);
simpleSkeletonManualObject->begin("vehicle-skeletonview-material", RenderOperation::OT_LINE_LIST);
for (int i=0; i<free_beam; i++)
{
simpleSkeletonManualObject->position(beams[i].p1->smoothpos);
simpleSkeletonManualObject->colour(1.0f,1.0f,1.0f);
simpleSkeletonManualObject->position(beams[i].p2->smoothpos);
simpleSkeletonManualObject->colour(0.0f,0.0f,0.0f);
}
simpleSkeletonManualObject->end();
simpleSkeletonNode->attachObject(simpleSkeletonManualObject);
simpleSkeletonInitiated = true;
}
void Beam::updateSimpleSkeleton()
{
BES_GFX_START(BES_GFX_UpdateSkeleton);
ColourValue color;
if (!simpleSkeletonInitiated)
initSimpleSkeleton();
simpleSkeletonManualObject->beginUpdate(0);
for (int i=0; i<free_beam; i++)
{
float stress_ratio = beams[i].stress / beams[i].minmaxposnegstress;
float color_scale = std::abs(stress_ratio);
color_scale = std::min(color_scale, 1.0f);
if (stress_ratio <= 0)
color = ColourValue(0.2f, 1.0f - color_scale, color_scale, 0.8f);
else
color = ColourValue(color_scale, 1.0f - color_scale, 0.2f, 0.8f);
simpleSkeletonManualObject->position(beams[i].p1->smoothpos);
simpleSkeletonManualObject->colour(color);
// remove broken beams
if (beams[i].broken || beams[i].disabled)
simpleSkeletonManualObject->position(beams[i].p1->smoothpos);
else
simpleSkeletonManualObject->position(beams[i].p2->smoothpos);
simpleSkeletonManualObject->colour(color);
}
simpleSkeletonManualObject->end();
BES_GFX_STOP(BES_GFX_UpdateSkeleton);
}
void Beam::moveOrigin(Vector3 offset)
{
origin += offset;
for (int i=0; i<free_node; i++)
{
nodes[i].RelPosition -= offset;
}
}
void Beam::changeOrigin(Vector3 newOrigin)
{
moveOrigin(newOrigin - origin);
}
float Beam::getRotation()
{
Vector3 cur_dir = getDirection();
return atan2(cur_dir.dotProduct(Vector3::UNIT_X), cur_dir.dotProduct(-Vector3::UNIT_Z));
}
Vector3 Beam::getDirection()
{
Vector3 cur_dir = nodes[0].AbsPosition;
if (cameranodepos[0] != cameranodedir[0] && cameranodepos[0] >= 0 && cameranodepos[0] < MAX_NODES && cameranodedir[0] >= 0 && cameranodedir[0] < MAX_NODES)
{
cur_dir = nodes[cameranodepos[0]].RelPosition - nodes[cameranodedir[0]].RelPosition;
} else if (free_node > 1)
{
float max_dist = 0.0f;
int furthest_node = 1;
for (int i=0; i<free_node; i++)
{
float dist = nodes[i].RelPosition.squaredDistance(nodes[0].RelPosition);
if (dist > max_dist)
{
max_dist = dist;
furthest_node = i;
}
}
cur_dir = nodes[0].RelPosition - nodes[furthest_node].RelPosition;
}
cur_dir.normalise();
return cur_dir;
}
Vector3 Beam::getPosition()
{
return position; //the position is already in absolute position
}
void Beam::CreateSimpleSkeletonMaterial()
{
if (MaterialManager::getSingleton().resourceExists("vehicle-skeletonview-material"))
{
return;
}
MaterialPtr mat=(MaterialPtr)(MaterialManager::getSingleton().create("vehicle-skeletonview-material", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME));
mat->getTechnique(0)->getPass(0)->createTextureUnitState();
mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureFiltering(TFO_ANISOTROPIC);
mat->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureAnisotropy(3);
mat->setLightingEnabled(false);
mat->setReceiveShadows(false);
}
void Beam::activate()
{
if (state < NETWORKED)
{
state = ACTIVATED;
}
}
void Beam::desactivate()
{
if (state < NETWORKED)
{
state = DESACTIVATED;
sleepcount = 0;
}
}
//called by the network thread
void Beam::pushNetwork(char* data, int size)
{
BES_GFX_START(BES_GFX_pushNetwork);
if (!oob3) return;
// check if the size of the data matches to what we expected
if ((unsigned int)size == (netbuffersize + sizeof(oob_t)))
{
// we walk through the incoming data and separate it a bit
char *ptr = data;
// put the oob_t in front, describes truck basics, engine state, flares, etc
memcpy((char*)oob3, ptr, sizeof(oob_t));
ptr += sizeof(oob_t);
// then copy the node data
memcpy((char*)netb3, ptr, nodebuffersize);
ptr += nodebuffersize;
// then take care of the wheel speeds
for (int i = 0; i < free_wheel; i++)
{
float wspeed = *(float*)(ptr);
wheels[i].rp3 = wspeed;
ptr += sizeof(float);
}
} else
{
// TODO: show the user the problem in the GUI
LOG("WRONG network size: we expected " + TOSTRING(netbuffersize+sizeof(oob_t)) + " but got " + TOSTRING(size) + " for vehicle " + String(truckname));
state = NETWORKED_INVALID;
return;
}
std::lock_guard<std::mutex> net_lock(m_net_mutex);
// and the buffer switching to have linear smoothing
oob_t *ot;
ot = oob1;
oob1 = oob2;
oob2 = oob3;
oob3 = ot;
char *ft;
ft = netb1;
netb1 = netb2;
netb2 = netb3;
netb3 = ft;
for (int i =0 ; i < free_wheel; i++)
{
float rp;
rp = wheels[i].rp1;
wheels[i].rp1 = wheels[i].rp2;
wheels[i].rp2 = wheels[i].rp3;
wheels[i].rp3 = rp;
}
netcounter++;
BES_GFX_STOP(BES_GFX_pushNetwork);
}
void Beam::calcNetwork()
{
if (netcounter != m_last_netcounter)
{
m_last_netcounter = netcounter;
m_last_net_update_time = getTruckTime();
}
BES_GFX_START(BES_GFX_calcNetwork);
if (netcounter < 4) return;
Vector3 apos=Vector3::ZERO;
//we must update Nodes positions from available network informations
//we must lock as long as we use oob1, oob2, netb1, netb2
std::lock_guard<std::mutex> net_lock(m_net_mutex);
int tnow=netTimer.getMilliseconds();
//adjust offset to match remote time
int rnow=tnow+net_toffset;
//if we receive older data from the future, we must correct the offset
if (oob1->time>rnow) {net_toffset=oob1->time-tnow; rnow=tnow+net_toffset;}
//if we receive last data from the past, we must correct the offset
if (oob2->time<rnow) {net_toffset=oob2->time-tnow; rnow=tnow+net_toffset;}
float tratio=(float)(rnow-oob1->time)/(float)(oob2->time-oob1->time);
//LOG(" network time diff: "+ TOSTRING(net_toffset));
Vector3 p1ref = Vector3::ZERO;
Vector3 p2ref = Vector3::ZERO;
short *sp1 = (short*)(netb1 + sizeof(float) * 3);
short *sp2 = (short*)(netb2 + sizeof(float) * 3);
Vector3 p1 = Vector3::ZERO;
Vector3 p2 = Vector3::ZERO;
for (int i = 0; i < first_wheel_node; i++)
{
//linear interpolation
if (i == 0)
{
// first node is uncompressed
p1.x = ((float*)netb1)[0];
p1.y = ((float*)netb1)[1];
p1.z = ((float*)netb1)[2];
p1ref = p1;
p2.x = ((float*)netb2)[0];
p2.y = ((float*)netb2)[1];
p2.z = ((float*)netb2)[2];
p2ref = p2;
}
else
{
// all other nodes are compressed:
// short int compared to previous node
p1.x = (float)(sp1[(i - 1) *3 + 0]) / 300.0f;
p1.y = (float)(sp1[(i - 1) *3 + 1]) / 300.0f;
p1.z = (float)(sp1[(i - 1) *3 + 2]) / 300.0f;
p1 = p1 + p1ref;
p2.x = (float)(sp2[(i - 1) *3 + 0]) / 300.0f;
p2.y = (float)(sp2[(i - 1) *3 + 1]) / 300.0f;
p2.z = (float)(sp2[(i - 1) *3 + 2]) / 300.0f;
p2 = p2 + p2ref;
}
nodes[i].AbsPosition = p1 + tratio * (p2 - p1);
nodes[i].smoothpos = nodes[i].AbsPosition;
nodes[i].RelPosition = nodes[i].AbsPosition - origin;
// calculate the average beside ...
apos += nodes[i].AbsPosition;
}
// set average position
position = apos / first_wheel_node;
// take care of the wheels
for (int i=0; i<free_wheel; i++)
{
float rp=wheels[i].rp1+tratio*(wheels[i].rp2-wheels[i].rp1);
//compute ideal positions
Vector3 axis=wheels[i].refnode1->RelPosition-wheels[i].refnode0->RelPosition;
axis.normalise();
Plane pplan=Plane(axis, wheels[i].refnode0->AbsPosition);
Vector3 ortho=-pplan.projectVector(wheels[i].near_attach->AbsPosition)-wheels[i].refnode0->AbsPosition;
Vector3 ray=ortho.crossProduct(axis);
ray.normalise();
ray=ray*wheels[i].radius;
float drp=2.0*3.14159/(wheels[i].nbnodes/2);
for (int j=0; j<wheels[i].nbnodes/2; j++)
{
Vector3 uray=Quaternion(Radian(rp-drp*j), axis)*ray;
wheels[i].nodes[j*2]->AbsPosition=wheels[i].refnode0->AbsPosition+uray;
wheels[i].nodes[j*2]->smoothpos=wheels[i].nodes[j*2]->AbsPosition;
wheels[i].nodes[j*2]->RelPosition=wheels[i].nodes[j*2]->AbsPosition-origin;
wheels[i].nodes[j*2+1]->AbsPosition=wheels[i].refnode1->AbsPosition+uray;
wheels[i].nodes[j*2+1]->smoothpos=wheels[i].nodes[j*2+1]->AbsPosition;
wheels[i].nodes[j*2+1]->RelPosition=wheels[i].nodes[j*2+1]->AbsPosition-origin;
}
}
//give some slack to the mutex
float engspeed = oob1->engine_speed+tratio*(oob2->engine_speed-oob1->engine_speed);
float engforce = oob1->engine_force+tratio*(oob2->engine_force-oob1->engine_force);
float engclutch = oob1->engine_clutch+tratio*(oob2->engine_clutch-oob1->engine_clutch);
float netwspeed = oob1->wheelspeed+tratio*(oob2->wheelspeed-oob1->wheelspeed);
float netbrake = oob1->brake+tratio*(oob2->brake-oob1->brake);
hydrodirwheeldisplay = oob1->hydrodirstate;
WheelSpeed = netwspeed;
int gear = oob1->engine_gear;
unsigned int flagmask = oob1->flagmask;
#ifdef USE_OPENAL
if (engine)
{
SoundScriptManager::getSingleton().modulate(trucknum, SS_MOD_ENGINE, engspeed);
}
if (free_aeroengine > 0)
{
SoundScriptManager::getSingleton().modulate(trucknum, SS_MOD_AEROENGINE1, engspeed);
SoundScriptManager::getSingleton().modulate(trucknum, SS_MOD_AEROENGINE2, engspeed);
SoundScriptManager::getSingleton().modulate(trucknum, SS_MOD_AEROENGINE3, engspeed);
SoundScriptManager::getSingleton().modulate(trucknum, SS_MOD_AEROENGINE4, engspeed);
}
#endif //OPENAL
brake = netbrake;
if (engine)
{
int automode = -1;
if ((flagmask&NETMASK_ENGINE_MODE_AUTOMATIC)!=0) automode = BeamEngine::AUTOMATIC;
else if ((flagmask&NETMASK_ENGINE_MODE_SEMIAUTO)!=0) automode = BeamEngine::SEMIAUTO;
else if ((flagmask&NETMASK_ENGINE_MODE_MANUAL)!=0) automode = BeamEngine::MANUAL;
else if ((flagmask&NETMASK_ENGINE_MODE_MANUAL_STICK)!=0) automode = BeamEngine::MANUAL_STICK;
else if ((flagmask&NETMASK_ENGINE_MODE_MANUAL_RANGES)!=0) automode = BeamEngine::MANUAL_RANGES;
bool contact = ((flagmask&NETMASK_ENGINE_CONT) != 0);
bool running = ((flagmask&NETMASK_ENGINE_RUN) != 0);
engine->netForceSettings(engspeed, engforce, engclutch, gear, running, contact, automode);
}
// set particle cannon
if (((flagmask&NETMASK_PARTICLE)!=0) != cparticle_mode)
toggleCustomParticles();
// set lights
if (((flagmask&NETMASK_LIGHTS)!=0) != lights)
lightsToggle();
if (((flagmask&NETMASK_BEACONS)!=0) != m_beacon_light_is_active)
beaconsToggle();
antilockbrake = flagmask & NETMASK_ALB_ACTIVE;
tractioncontrol = flagmask & NETMASK_TC_ACTIVE;
parkingbrake = flagmask & NETMASK_PBRAKE;
blinktype btype = BLINK_NONE;
if ((flagmask&NETMASK_BLINK_LEFT)!=0)
btype = BLINK_LEFT;
else if ((flagmask&NETMASK_BLINK_RIGHT)!=0)
btype = BLINK_RIGHT;
else if ((flagmask&NETMASK_BLINK_WARN)!=0)
btype = BLINK_WARN;
setBlinkType(btype);
setCustomLightVisible(0, ((flagmask&NETMASK_CLIGHT1)>0));
setCustomLightVisible(1, ((flagmask&NETMASK_CLIGHT2)>0));
setCustomLightVisible(2, ((flagmask&NETMASK_CLIGHT3)>0));
setCustomLightVisible(3, ((flagmask&NETMASK_CLIGHT4)>0));
#ifdef USE_OPENAL
if ((flagmask & NETMASK_HORN))
SoundScriptManager::getSingleton().trigStart(trucknum, SS_TRIG_HORN);
else
SoundScriptManager::getSingleton().trigStop(trucknum, SS_TRIG_HORN);
#endif //OPENAL
netBrakeLight = ((flagmask&NETMASK_BRAKES)!=0);
netReverseLight = ((flagmask&NETMASK_REVERSE)!=0);
#ifdef USE_OPENAL
if (netReverseLight)
SoundScriptManager::getSingleton().trigStart(trucknum, SS_TRIG_REVERSE_GEAR);
else
SoundScriptManager::getSingleton().trigStop(trucknum, SS_TRIG_REVERSE_GEAR);
#endif //OPENAL
#ifdef USE_MYGUI
updateDashBoards(tratio);
#endif // USE_MYGUI
BES_GFX_STOP(BES_GFX_calcNetwork);
}
bool Beam::addPressure(float v)
{
if (!free_pressure_beam)
return false;
float newpressure = std::max(0.0f, std::min(refpressure + v, 100.0f));
if (newpressure == refpressure)
return false;
refpressure = newpressure;
for (int i=0; i<free_pressure_beam; i++)
{
beams[pressure_beams[i]].k = 10000 + refpressure * 10000;
}
return true;
}
float Beam::getPressure()
{
if (free_pressure_beam) return refpressure;
return 0;
}
void Beam::calc_masses2(Real total, bool reCalc)
{
BES_GFX_START(BES_GFX_calc_masses2);
bool debugMass = BSETTING("Debug Truck Mass", false);
//reset
for (int i=0; i<free_node; i++)
{
if (!nodes[i].iswheel)
{
if (!nodes[i].loadedMass)
{
nodes[i].mass = 0;
} else if (!nodes[i].overrideMass)
{
nodes[i].mass = loadmass / (float)masscount;
}
}
}
//average linear density
Real len = 0.0f;
for (int i=0; i<free_beam; i++)
{
if (beams[i].type!=BEAM_VIRTUAL)
{
Real half_newlen = beams[i].L / 2.0;
if (!(beams[i].p1->iswheel)) len += half_newlen;
if (!(beams[i].p2->iswheel)) len += half_newlen;
}
}
if (!reCalc)
{
for (int i=0; i<free_beam; i++)
{
if (beams[i].type!=BEAM_VIRTUAL)
{
Real half_mass = beams[i].L * total / len / 2.0f;
if (!(beams[i].p1->iswheel)) beams[i].p1->mass += half_mass;
if (!(beams[i].p2->iswheel)) beams[i].p2->mass += half_mass;
}
}
}
//fix rope masses
for (std::vector <rope_t>::iterator it = ropes.begin(); it!=ropes.end(); it++)
{
it->beam->p2->mass = 100.0f;
}
//fix camera mass
for (int i=0; i<freecinecamera; i++)
nodes[cinecameranodepos[i]].mass = 20.0f;
//hooks must be heavy
//for (std::vector<hook_t>::iterator it=hooks.begin(); it!=hooks.end(); it++)
// if (!it->hookNode->overrideMass)
// it->hookNode->mass = 500.0f;
//update mass
for (int i=0; i<free_node; i++)
{
//LOG("Nodemass "+TOSTRING(i)+"-"+TOSTRING(nodes[i].mass));
//for stability
if (!nodes[i].iswheel && nodes[i].mass < minimass)
{
if (debugMass)
LOG("Node " + TOSTRING(i) +" mass ("+TOSTRING(nodes[i].mass)+"kg) too light. Resetting to minimass ("+ TOSTRING(minimass) +"kg).");
nodes[i].mass = minimass;
}
}
totalmass = 0;
for (int i=0; i<free_node; i++)
{
if (debugMass)
{
String msg = "Node " + TOSTRING(i) +" : "+ TOSTRING((int)nodes[i].mass) +" kg";
if (nodes[i].loadedMass)
{
if (nodes[i].overrideMass)
msg += " (overriden by node mass)";
else
msg += " (normal load node: "+TOSTRING(loadmass)+" kg / "+TOSTRING(masscount)+" nodes)";
}
LOG(msg);
}
totalmass += nodes[i].mass;
}
LOG("TOTAL VEHICLE MASS: " + TOSTRING((int)totalmass) +" kg");
BES_GFX_STOP(BES_GFX_calc_masses2);
}
// this recalculates the masses (useful when the gravity was changed...)
void Beam::recalc_masses()
{
this->calc_masses2(totalmass, true);
}
float Beam::getTotalMass(bool withLocked)
{
if (!withLocked) return totalmass; // already computed in calc_masses2
float mass = totalmass;
for (std::list<Beam*>::iterator it = linkedBeams.begin(); it != linkedBeams.end(); ++it)
{
mass += (*it)->totalmass;
}
return mass;
}
void Beam::determineLinkedBeams()
{
linkedBeams.clear();
bool found = true;
std::map< Beam*, bool> lookup_table;
std::pair<std::map< Beam*, bool>::iterator, bool> ret;
lookup_table.insert(std::pair< Beam*, bool>(this, false));
while (found)
{
found = false;
for (std::map< Beam*, bool>::iterator it_beam=lookup_table.begin(); it_beam != lookup_table.end(); ++it_beam)
{
if (!it_beam->second)
{
for (std::vector<hook_t>::iterator it_hook=it_beam->first->hooks.begin(); it_hook != it_beam->first->hooks.end(); ++it_hook)
{
if (it_hook->lockTruck)
{
ret = lookup_table.insert(std::pair< Beam*, bool>(it_hook->lockTruck, false));
if (ret.second)
{
linkedBeams.push_back(it_hook->lockTruck);
found = true;
}
}
}
it_beam->second = true;
}
}