/
KX_Scene.cpp
2339 lines (1931 loc) · 65.8 KB
/
KX_Scene.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
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
* Ketsji scene. Holds references to all scene data.
*/
/** \file gameengine/Ketsji/KX_Scene.cpp
* \ingroup ketsji
*/
#ifdef _MSC_VER
# pragma warning (disable:4786)
#endif
#include "KX_Scene.h"
#include "KX_Globals.h"
#include "BLI_utildefines.h"
#include "KX_KetsjiEngine.h"
#include "KX_BlenderMaterial.h"
#include "KX_TextMaterial.h"
#include "KX_FontObject.h"
#include "RAS_IMaterial.h"
#include "EXP_ListValue.h"
#include "SCA_LogicManager.h"
#include "SCA_TimeEventManager.h"
#include "SCA_2DFilterActuator.h"
#include "SCA_PythonController.h"
#include "KX_CollisionEventManager.h"
#include "SCA_KeyboardManager.h"
#include "SCA_MouseManager.h"
#include "SCA_ActuatorEventManager.h"
#include "SCA_BasicEventManager.h"
#include "KX_Camera.h"
#include "KX_NavMeshObject.h"
#include "SCA_JoystickManager.h"
#include "KX_PyMath.h"
#include "KX_Mesh.h"
#include "SCA_IScene.h"
#include "KX_LodManager.h"
#include "KX_CullingHandler.h"
#include "RAS_Rasterizer.h"
#include "RAS_ICanvas.h"
#include "RAS_2DFilterData.h"
#include "KX_2DFilterManager.h"
#include "RAS_BoundingBoxManager.h"
#include "RAS_BucketManager.h"
#include "RAS_Deformer.h"
#include "EXP_FloatValue.h"
#include "SCA_IController.h"
#include "SCA_IActuator.h"
#include "SG_Node.h"
#include "SG_Controller.h"
#include "SG_Node.h"
#include "DNA_group_types.h"
#include "DNA_scene_types.h"
#include "DNA_property_types.h"
#include "KX_NodeRelationships.h"
#include "KX_NetworkMessageScene.h"
#include "PHY_IPhysicsEnvironment.h"
#include "PHY_IGraphicController.h"
#include "PHY_IPhysicsController.h"
#include "BL_Converter.h"
#include "BL_ArmatureObject.h"
#include "KX_MotionState.h"
#include "KX_ObstacleSimulation.h"
#ifdef WITH_PYTHON
# include "EXP_PythonCallBack.h"
#endif
#include "KX_LightObject.h"
#include "BLI_task.h"
#include "CM_Message.h"
#include "CM_List.h"
static void *KX_SceneReplicationFunc(SG_Node *node, void *gameobj, void *scene)
{
KX_GameObject *replica = ((KX_Scene *)scene)->AddNodeReplicaObject(node, (KX_GameObject *)gameobj);
if (replica) {
replica->Release();
}
return (void *)replica;
}
static void *KX_SceneDestructionFunc(SG_Node *node, void *gameobj, void *scene)
{
((KX_Scene *)scene)->RemoveNodeDestructObject((KX_GameObject *)gameobj);
return nullptr;
}
bool KX_Scene::KX_ScenegraphUpdateFunc(SG_Node *node, void *gameobj, void *scene)
{
return node->Schedule(((KX_Scene *)scene)->m_sghead);
}
bool KX_Scene::KX_ScenegraphRescheduleFunc(SG_Node *node, void *gameobj, void *scene)
{
return node->Reschedule(((KX_Scene *)scene)->m_sghead);
}
SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(
KX_SceneReplicationFunc,
KX_SceneDestructionFunc,
KX_GameObject::UpdateTransformFunc,
KX_Scene::KX_ScenegraphUpdateFunc,
KX_Scene::KX_ScenegraphRescheduleFunc);
KX_Scene::KX_Scene(SCA_IInputDevice *inputDevice,
const std::string& sceneName,
Scene *scene,
RAS_ICanvas *canvas,
KX_NetworkMessageManager *messageManager) :
m_keyboardmgr(nullptr),
m_mousemgr(nullptr),
m_physicsEnvironment(0),
m_sceneName(sceneName),
m_activeCamera(nullptr),
m_overrideCullingCamera(nullptr),
m_ueberExecutionPriority(0),
m_suspend(false),
m_suspendedDelta(0.0),
m_activityCulling(false),
m_dbvtCulling(false),
m_dbvtOcclusionRes(0),
m_blenderScene(scene),
m_previousAnimTime(0.0f),
m_isActivedHysteresis(false),
m_lodHysteresisValue(0)
{
m_objectlist = new EXP_ListValue<KX_GameObject>();
m_parentlist = new EXP_ListValue<KX_GameObject>();
m_lightlist = new EXP_ListValue<KX_LightObject>();
m_inactivelist = new EXP_ListValue<KX_GameObject>();
m_cameralist = new EXP_ListValue<KX_Camera>();
m_fontlist = new EXP_ListValue<KX_FontObject>();
bool useFxaa = (scene->gm.aasamples == 1);
m_filterManager = new KX_2DFilterManager(useFxaa);
m_logicmgr = new SCA_LogicManager();
m_timemgr = new SCA_TimeEventManager(m_logicmgr);
m_keyboardmgr = new SCA_KeyboardManager(m_logicmgr, inputDevice);
m_mousemgr = new SCA_MouseManager(m_logicmgr, inputDevice);
SCA_ActuatorEventManager *actmgr = new SCA_ActuatorEventManager(m_logicmgr);
SCA_BasicEventManager *basicmgr = new SCA_BasicEventManager(m_logicmgr);
m_logicmgr->RegisterEventManager(actmgr);
m_logicmgr->RegisterEventManager(m_keyboardmgr);
m_logicmgr->RegisterEventManager(m_mousemgr);
m_logicmgr->RegisterEventManager(m_timemgr);
m_logicmgr->RegisterEventManager(basicmgr);
SCA_JoystickManager *joymgr = new SCA_JoystickManager(m_logicmgr);
m_logicmgr->RegisterEventManager(joymgr);
m_networkScene = new KX_NetworkMessageScene(messageManager);
m_rendererManager = new KX_TextureRendererManager(this);
m_bucketmanager = new RAS_BucketManager(KX_TextMaterial::GetSingleton());
m_boundingBoxManager = new RAS_BoundingBoxManager();
m_animationPool = BLI_task_pool_create(KX_GetActiveEngine()->GetTaskScheduler(), &m_animationPoolData);
#ifdef WITH_PYTHON
m_attrDict = nullptr;
m_removeCallbacks = nullptr;
for (unsigned short i = 0; i < MAX_DRAW_CALLBACK; ++i) {
m_drawCallbacks[i] = nullptr;
}
#endif
}
KX_Scene::~KX_Scene()
{
/* The release of debug properties used to be in SCA_IScene::~SCA_IScene
* It's still there but we remove all properties here otherwise some
* reference might be hanging and causing late release of objects
*/
RemoveAllDebugProperties();
while (GetRootParentList()->GetCount() > 0) {
KX_GameObject *parentobj = GetRootParentList()->GetValue(0);
this->RemoveObject(parentobj);
}
if (m_obstacleSimulation) {
delete m_obstacleSimulation;
}
if (m_animationPool) {
BLI_task_pool_free(m_animationPool);
}
if (m_objectlist) {
m_objectlist->Release();
}
if (m_parentlist) {
m_parentlist->Release();
}
if (m_inactivelist) {
m_inactivelist->Release();
}
if (m_lightlist) {
m_lightlist->Release();
}
if (m_cameralist) {
m_cameralist->Release();
}
if (m_fontlist) {
m_fontlist->Release();
}
if (m_filterManager) {
delete m_filterManager;
}
if (m_logicmgr) {
delete m_logicmgr;
}
if (m_physicsEnvironment) {
delete m_physicsEnvironment;
}
if (m_networkScene) {
delete m_networkScene;
}
if (m_rendererManager) {
delete m_rendererManager;
}
if (m_bucketmanager) {
delete m_bucketmanager;
}
if (m_boundingBoxManager) {
delete m_boundingBoxManager;
}
if (m_worldinfo) {
delete m_worldinfo;
}
#ifdef WITH_PYTHON
if (m_attrDict) {
PyDict_Clear(m_attrDict);
Py_CLEAR(m_attrDict);
}
// These may be nullptr but the macro checks.
Py_CLEAR(m_removeCallbacks);
for (unsigned short i = 0; i < MAX_DRAW_CALLBACK; ++i) {
Py_CLEAR(m_drawCallbacks[i]);
}
#endif
}
std::string KX_Scene::GetName()
{
return m_sceneName;
}
void KX_Scene::SetName(const std::string& name)
{
m_sceneName = name;
}
RAS_BucketManager *KX_Scene::GetBucketManager() const
{
return m_bucketmanager;
}
KX_TextureRendererManager *KX_Scene::GetTextureRendererManager() const
{
return m_rendererManager;
}
RAS_BoundingBoxManager *KX_Scene::GetBoundingBoxManager() const
{
return m_boundingBoxManager;
}
EXP_ListValue<KX_GameObject> *KX_Scene::GetObjectList() const
{
return m_objectlist;
}
EXP_ListValue<KX_GameObject> *KX_Scene::GetRootParentList() const
{
return m_parentlist;
}
EXP_ListValue<KX_GameObject> *KX_Scene::GetInactiveList() const
{
return m_inactivelist;
}
EXP_ListValue<KX_LightObject> *KX_Scene::GetLightList() const
{
return m_lightlist;
}
EXP_ListValue<KX_Camera> *KX_Scene::GetCameraList() const
{
return m_cameralist;
}
EXP_ListValue<KX_FontObject> *KX_Scene::GetFontList() const
{
return m_fontlist;
}
SCA_LogicManager *KX_Scene::GetLogicManager() const
{
return m_logicmgr;
}
SCA_TimeEventManager *KX_Scene::GetTimeEventManager() const
{
return m_timemgr;
}
KX_PythonComponentManager& KX_Scene::GetPythonComponentManager()
{
return m_componentManager;
}
void KX_Scene::SetFramingType(const RAS_FrameSettings& frameSettings)
{
m_frameSettings = frameSettings;
}
const RAS_FrameSettings& KX_Scene::GetFramingType() const
{
return m_frameSettings;
}
void KX_Scene::SetWorldInfo(KX_WorldInfo *worldinfo)
{
m_worldinfo = worldinfo;
}
KX_WorldInfo *KX_Scene::GetWorldInfo() const
{
return m_worldinfo;
}
void KX_Scene::Suspend()
{
m_suspend = true;
}
void KX_Scene::Resume()
{
m_suspend = false;
}
void KX_Scene::SetActivityCulling(bool b)
{
m_activityCulling = b;
}
bool KX_Scene::IsSuspended() const
{
return m_suspend;
}
void KX_Scene::SetDbvtCulling(bool b)
{
m_dbvtCulling = b;
}
bool KX_Scene::GetDbvtCulling() const
{
return m_dbvtCulling;
}
void KX_Scene::SetDbvtOcclusionRes(int i)
{
m_dbvtOcclusionRes = i;
}
int KX_Scene::GetDbvtOcclusionRes() const
{
return m_dbvtOcclusionRes;
}
void KX_Scene::AddObjectDebugProperties(KX_GameObject *gameobj)
{
Object *blenderobject = gameobj->GetBlenderObject();
if (!blenderobject) {
return;
}
for (bProperty *prop = (bProperty *)blenderobject->prop.first; prop; prop = prop->next) {
if (prop->flag & PROP_DEBUG) {
AddDebugProperty(gameobj, prop->name);
}
}
if (blenderobject->scaflag & OB_DEBUGSTATE) {
AddDebugProperty(gameobj, "__state__");
}
}
void KX_Scene::RemoveNodeDestructObject(KX_GameObject *gameobj)
{
if (NewRemoveObject(gameobj)) {
/* Object is not yet deleted because a reference is hanging somewhere.
* This should not happen anymore since we use proxy object for Python. */
CM_Error("zombie object! name=" << gameobj->GetName());
BLI_assert(false);
}
}
KX_GameObject *KX_Scene::AddNodeReplicaObject(SG_Node *node, KX_GameObject *gameobj)
{
/* For group duplication, limit the duplication of the hierarchy to the
* objects that are part of the group. */
if (!IsObjectInGroup(gameobj)) {
return nullptr;
}
KX_GameObject *newobj = static_cast<KX_GameObject *>(gameobj->GetReplica());
m_map_gameobject_to_replica[gameobj] = newobj;
// Also register 'timers' (time properties) of the replica.
for (unsigned short i = 0, numprops = newobj->GetPropertyCount(); i < numprops; ++i) {
EXP_Value *prop = newobj->GetProperty(i);
if (prop->GetProperty("timer")) {
m_timemgr->AddTimeProperty(prop);
}
}
if (node) {
newobj->SetNode(node);
}
else {
SG_Node *rootnode = new SG_Node(newobj, this, KX_Scene::m_callbacks);
// This fixes part of the scaling-added object bug.
SG_Node *orgnode = gameobj->GetNode();
rootnode->SetLocalScale(orgnode->GetLocalScale());
rootnode->SetLocalPosition(orgnode->GetLocalPosition());
rootnode->SetLocalOrientation(orgnode->GetLocalOrientation());
// Define the relationship between this node and it's parent.
KX_NormalParentRelation *parent_relation = new KX_NormalParentRelation();
rootnode->SetParentRelation(parent_relation);
newobj->SetNode(rootnode);
}
SG_Node *replicanode = newobj->GetNode();
// Add the object in the obstacle simulation if needed.
if (m_obstacleSimulation && gameobj->GetBlenderObject()->gameflag & OB_HASOBSTACLE) {
m_obstacleSimulation->AddObstacleForObj(newobj);
}
// Reconstruct nav mesh.
if (gameobj->GetGameObjectType() == SCA_IObject::OBJ_NAVMESH) {
static_cast<KX_NavMeshObject *>(gameobj)->BuildNavMesh();
}
// Register object for component update.
if (gameobj->GetComponents()) {
m_componentManager.RegisterObject(newobj);
}
replicanode->SetClientObject(newobj);
// This is the list of object that are send to the graphics pipeline.
m_objectlist->Add(CM_AddRef(newobj));
switch (newobj->GetGameObjectType()) {
case SCA_IObject::OBJ_LIGHT:
{
m_lightlist->Add(CM_AddRef(static_cast<KX_LightObject *>(newobj)));
break;
}
case SCA_IObject::OBJ_TEXT:
{
m_fontlist->Add(CM_AddRef(static_cast<KX_FontObject *>(newobj)));
break;
}
case SCA_IObject::OBJ_CAMERA:
{
m_cameralist->Add(CM_AddRef(static_cast<KX_Camera *>(newobj)));
break;
}
case SCA_IObject::OBJ_ARMATURE:
{
AddAnimatedObject(newobj);
break;
}
}
// Logic cannot be replicated, until the whole hierarchy is replicated.
m_logicHierarchicalGameObjects.push_back(newobj);
// Replicate graphic controller.
if (gameobj->GetGraphicController()) {
PHY_IMotionState *motionstate = new KX_MotionState(newobj->GetNode());
PHY_IGraphicController *newctrl = gameobj->GetGraphicController()->GetReplica(motionstate);
newctrl->SetNewClientInfo(&newobj->GetClientInfo());
newobj->SetGraphicController(newctrl);
}
// Replicate physics controller.
if (gameobj->GetPhysicsController()) {
PHY_IMotionState *motionstate = new KX_MotionState(newobj->GetNode());
PHY_IPhysicsController *newctrl = gameobj->GetPhysicsController()->GetReplica();
KX_GameObject *parent = newobj->GetParent();
PHY_IPhysicsController *parentctrl = (parent) ? parent->GetPhysicsController() : nullptr;
newctrl->SetNewClientInfo(&newobj->GetClientInfo());
newobj->SetPhysicsController(newctrl);
newctrl->PostProcessReplica(motionstate, parentctrl);
// Child objects must be static.
if (parent) {
newctrl->SuspendDynamics();
}
}
return newobj;
}
/*
* Before calling this method KX_Scene::ReplicateLogic(), make sure to
* have called 'GameObject::ReParentLogic' for each object this
* hierarchy that's because first ALL bricks must exist in the new
* replica of the hierarchy in order to make cross-links work properly.
*
* It is VERY important that the order of sensors and actuators in
* the replicated object is preserved: it is used to reconnect the logic.
* This method is more robust then using the bricks name in case of complex
* group replication. The replication of logic bricks is done in
* SCA_IObject::ReParentLogic(), make sure it preserves the order of the bricks.
*/
void KX_Scene::ReplicateLogic(KX_GameObject *newobj)
{
// Add properties to debug list, for added objects and DupliGroups.
if (KX_GetActiveEngine()->GetFlag(KX_KetsjiEngine::AUTO_ADD_DEBUG_PROPERTIES)) {
AddObjectDebugProperties(newobj);
}
// Also relink the controller to sensors/actuators.
const SCA_ControllerList controllers = newobj->GetControllers();
for (SCA_IController *cont : controllers) {
cont->SetUeberExecutePriority(m_ueberExecutionPriority);
const SCA_SensorList linkedsensors = cont->GetLinkedSensors();
const SCA_ActuatorList linkedactuators = cont->GetLinkedActuators();
/* Disconnect the sensors and actuators
* do it directly on the list at this controller is not connected to anything at this stage. */
cont->GetLinkedSensors().clear();
cont->GetLinkedActuators().clear();
// Now relink each sensor.
for (SCA_ISensor *oldsensor : linkedsensors) {
SCA_IObject *oldsensorobj = oldsensor->GetParent();
// The original owner of the sensor has been replicated?
SCA_IObject *newsensorobj = m_map_gameobject_to_replica[oldsensorobj];
if (!newsensorobj) {
// No, then the sensor points outside the hierarchy, keep it the same.
if (m_objectlist->SearchValue(static_cast<KX_GameObject *>(oldsensorobj))) {
// Only replicate links that points to active objects.
m_logicmgr->RegisterToSensor(cont, oldsensor);
}
}
else {
// Yes, then the new sensor has the same position.
SCA_SensorList& sensorlist = oldsensorobj->GetSensors();
SCA_SensorList::iterator sit;
SCA_ISensor *newsensor = nullptr;
int sensorpos;
for (sensorpos = 0, sit = sensorlist.begin(); sit != sensorlist.end(); sit++, sensorpos++) {
if ((*sit) == oldsensor) {
newsensor = newsensorobj->GetSensors().at(sensorpos);
break;
}
}
BLI_assert(newsensor != nullptr);
m_logicmgr->RegisterToSensor(cont, newsensor);
}
}
// Now relink each actuator.
for (SCA_IActuator *oldactuator : linkedactuators) {
SCA_IObject *oldactuatorobj = oldactuator->GetParent();
SCA_IObject *newactuatorobj = m_map_gameobject_to_replica[oldactuatorobj];
if (!newactuatorobj) {
// No, then the sensor points outside the hierarchy, keep it the same.
if (m_objectlist->SearchValue(static_cast<KX_GameObject *>(oldactuatorobj))) {
// Only replicate links that points to active objects
m_logicmgr->RegisterToActuator(cont, oldactuator);
}
}
else {
// Yes, then the new sensor has the same position
SCA_ActuatorList& actuatorlist = oldactuatorobj->GetActuators();
SCA_ActuatorList::iterator ait;
SCA_IActuator *newactuator = nullptr;
int actuatorpos;
for (actuatorpos = 0, ait = actuatorlist.begin(); ait != actuatorlist.end(); ait++, actuatorpos++) {
if ((*ait) == oldactuator) {
newactuator = newactuatorobj->GetActuators().at(actuatorpos);
break;
}
}
BLI_assert(newactuator != nullptr);
m_logicmgr->RegisterToActuator(cont, newactuator);
newactuator->SetUeberExecutePriority(m_ueberExecutionPriority);
}
}
}
// Ready to set initial state.
newobj->ResetState();
}
void KX_Scene::DupliGroupRecurse(KX_GameObject *groupobj, int level)
{
Object *blgroupobj = groupobj->GetBlenderObject();
std::vector<KX_GameObject *> duplilist;
if (!groupobj->GetNode() || !groupobj->IsDupliGroup() || level > MAX_DUPLI_RECUR) {
return;
}
// We will add one group at a time.
m_logicHierarchicalGameObjects.clear();
m_map_gameobject_to_replica.clear();
m_ueberExecutionPriority++;
/* For groups will do something special:
* we will force the creation of objects to those in the group only
* Again, this is match what Blender is doing (it doesn't care of parent relationship)
*/
m_groupGameObjects.clear();
Group *group = blgroupobj->dup_group;
for (GroupObject *go = (GroupObject *)group->gobject.first; go; go = (GroupObject *)go->next) {
Object *blenderobj = go->ob;
if (blgroupobj == blenderobj) {
// This check is also in group_duplilist().
continue;
}
KX_GameObject *gameobj = (KX_GameObject *)m_logicmgr->FindGameObjByBlendObj(blenderobj);
if (gameobj == nullptr) {
/* This object has not been converted.
* Should not happen as dupli group are created automatically */
continue;
}
if ((blenderobj->lay & group->layer) == 0) {
// Object is not visible in the 3D view, will not be instantiated.
continue;
}
m_groupGameObjects.insert(gameobj);
}
for (KX_GameObject *gameobj : m_groupGameObjects) {
KX_GameObject *parent = gameobj->GetParent();
if (parent != nullptr) {
/* This object is not a top parent. Either it is the child of another
* object in the group and it will be added automatically when the parent
* is added. Or it is the child of an object outside the group and the group
* is inconsistent, skip it anyway.
*/
continue;
}
KX_GameObject *replica = AddNodeReplicaObject(nullptr, gameobj);
// Add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame).
m_parentlist->Add(CM_AddRef(replica));
// Recurse replication into children nodes.
const NodeList& children = gameobj->GetNode()->GetChildren();
replica->GetNode()->ClearSGChildren();
for (SG_Node *orgnode : children) {
SG_Node *childreplicanode = orgnode->GetReplica();
if (childreplicanode) {
replica->GetNode()->AddChild(childreplicanode);
}
}
/* Don't replicate logic now: we assume that the objects in the group can have
* logic relationship, even outside parent relationship
* In order to match 3D view, the position of groupobj is used as a
* transformation matrix instead of the new position. This means that
* the group reference point is 0,0,0.
*/
// Get the rootnode's scale.
const mt::vec3& newscale = groupobj->NodeGetWorldScaling();
// Set the replica's relative scale with the rootnode's scale.
replica->NodeSetRelativeScale(newscale);
const mt::vec3 offset(group->dupli_ofs);
const mt::vec3 newpos = groupobj->NodeGetWorldPosition() +
newscale * (groupobj->NodeGetWorldOrientation() * (gameobj->NodeGetWorldPosition() - offset));
replica->NodeSetLocalPosition(newpos);
// Set the orientation after position for softbody.
const mt::mat3 newori = groupobj->NodeGetWorldOrientation() * gameobj->NodeGetWorldOrientation();
replica->NodeSetLocalOrientation(newori);
// Update scenegraph for entire tree of children.
replica->GetNode()->UpdateWorldData();
// We can now add the graphic controller to the physic engine.
replica->ActivateGraphicController(true);
// Done with replica.
replica->Release();
}
// Do the linking of member objects to group object for every objects.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
/* Set references for dupli-group
* groupobj holds a list of all objects, that belongs to this group. */
groupobj->AddInstanceObjects(gameobj);
// Every object gets the reference to its dupli-group object.
gameobj->SetDupliGroupObject(groupobj);
}
/* The logic must be replicated first because we need
* the new logic bricks before relinking. */
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
gameobj->ReParentLogic();
}
// Relink any pointers as necessary, sort of a temporary solution.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
// This will also relink the actuator to objects within the hierarchy.
gameobj->Relink(m_map_gameobject_to_replica);
gameobj->AddMeshUser();
// Always make sure that the bounding box is valid.
gameobj->UpdateBounds(true);
// Add the object in the layer of the parent.
gameobj->SetLayer(groupobj->GetLayer());
}
// Replicate crosslinks etc. between logic bricks.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
ReplicateLogic(gameobj);
}
// Now look if object in the hierarchy have dupli group and recurse.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
// Replicate all constraints.
gameobj->ReplicateConstraints(m_physicsEnvironment, m_logicHierarchicalGameObjects);
if (gameobj != groupobj && gameobj->IsDupliGroup()) {
// Can't instantiate group immediately as it destroys m_logicHierarchicalGameObjects.
duplilist.push_back(gameobj);
}
}
for (KX_GameObject *gameobj : duplilist) {
DupliGroupRecurse(gameobj, level + 1);
}
}
bool KX_Scene::IsObjectInGroup(KX_GameObject *gameobj) const
{
return (m_groupGameObjects.empty() || m_groupGameObjects.find(gameobj) != m_groupGameObjects.end());
}
KX_GameObject *KX_Scene::AddReplicaObject(KX_GameObject *originalobj, KX_GameObject *referenceobj, float lifespan)
{
m_logicHierarchicalGameObjects.clear();
m_map_gameobject_to_replica.clear();
m_groupGameObjects.clear();
m_ueberExecutionPriority++;
// Lets create a replica.
KX_GameObject *replica = AddNodeReplicaObject(nullptr, originalobj);
/* Add a timebomb to this object
* lifespan of zero means 'this object lives forever'. */
if (lifespan > 0.0f) {
// For now, convert between so called frames and realtime.
m_tempObjectList.push_back(replica);
/* This convert the life from frames to sort-of seconds, hard coded 0.02 that assumes we have 50 frames per second
* if you change this value, make sure you change it in KX_GameObject::pyattr_get_life property too. */
EXP_Value *fval = new EXP_FloatValue(lifespan * 0.02f);
replica->SetProperty("::timebomb", fval);
fval->Release();
}
// Add to 'rootparent' list (this is the list of top hierarchy objects, updated each frame).
m_parentlist->Add(CM_AddRef(replica));
// Recurse replication into children nodes.
const NodeList& children = originalobj->GetNode()->GetChildren();
replica->GetNode()->ClearSGChildren();
for (SG_Node *orgnode : children) {
SG_Node *childreplicanode = orgnode->GetReplica();
if (childreplicanode) {
replica->GetNode()->AddChild(childreplicanode);
}
}
if (referenceobj) {
/* At this stage all the objects in the hierarchy have been duplicated,
* we can update the scenegraph, we need it for the duplication of logic. */
const mt::vec3& newpos = referenceobj->NodeGetWorldPosition();
replica->NodeSetLocalPosition(newpos);
const mt::mat3& newori = referenceobj->NodeGetWorldOrientation();
replica->NodeSetLocalOrientation(newori);
// Get the rootnode's scale.
const mt::vec3& newscale = referenceobj->GetNode()->GetRootSGParent()->GetLocalScale();
// Set the replica's relative scale with the rootnode's scale.
replica->NodeSetRelativeScale(newscale);
}
replica->GetNode()->UpdateWorldData();
// The size is correct, we can add the graphic controller to the physic engine.
replica->ActivateGraphicController(true);
// Now replicate logic.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
gameobj->ReParentLogic();
}
// Relink any pointers as necessary, sort of a temporary solution.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
// This will also relink the actuators in the hierarchy.
gameobj->Relink(m_map_gameobject_to_replica);
gameobj->AddMeshUser();
// Always make sure that the bounding box is valid.
gameobj->UpdateBounds(true);
if (referenceobj) {
// Add the object in the layer of the reference object.
gameobj->SetLayer(referenceobj->GetLayer());
}
else {
// We don't know what layer set, so we set all visible layers in the blender scene.
gameobj->SetLayer(m_blenderScene->lay);
}
}
// Replicate crosslinks etc. between logic bricks.
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
ReplicateLogic(gameobj);
}
// Check if there are objects with dupligroup in the hierarchy.
std::vector<KX_GameObject *> duplilist;
for (KX_GameObject *gameobj : m_logicHierarchicalGameObjects) {
if (gameobj->IsDupliGroup()) {
// Separate list as m_logicHierarchicalGameObjects is also used by DupliGroupRecurse().
duplilist.push_back(gameobj);
}
}
for (KX_GameObject *gameobj : duplilist) {
DupliGroupRecurse(gameobj, 0);
}
// Don't release replica here because we are returning it, not done with it...
return replica;
}
void KX_Scene::RemoveObject(KX_GameObject *gameobj)
{
// Disconnect child from parent.
SG_Node *node = gameobj->GetNode();
if (node) {
node->DisconnectFromParent();
// Recursively destruct.
node->Destruct();
}
}
void KX_Scene::RemoveDupliGroup(KX_GameObject *gameobj)
{
if (gameobj->GetInstanceObjects()) {
for (KX_GameObject *instance : gameobj->GetInstanceObjects()) {
DelayedRemoveObject(instance);
}
}
}
void KX_Scene::DelayedRemoveObject(KX_GameObject *gameobj)
{
RemoveDupliGroup(gameobj);
CM_ListAddIfNotFound(m_euthanasyobjects, gameobj);
}
void KX_Scene::RemoveEuthanasyObjects()
{
/* Don't remove the objects from the euthanasy list here as the child objects of a deleted
* parent object are destructed directly from the sgnode in the same time the parent
* object is destructed. These child objects must be removed automatically from the
* euthanasy list to avoid double deletion in case the user ask to delete the child object
* explicitly. NewRemoveObject is the place to do it.
*/
while (!m_euthanasyobjects.empty()) {
RemoveObject(m_euthanasyobjects.front());
}
}
bool KX_Scene::NewRemoveObject(KX_GameObject *gameobj)
{
// Remove property from debug list.
RemoveObjectDebugProperties(gameobj);
/* Invalidate the python reference, since the object may exist in script lists
* its possible that it wont be automatically invalidated, so do it manually here,
*
* if for some reason the object is added back into the scene python can always get a new Proxy
*/
gameobj->InvalidateProxy();
/* Keep the blender->game object association up to date
* note that all the replicas of an object will have the same
* blender object, that's why we need to check the game object
* as only the deletion of the original object must be recorded.
*/
if (gameobj->GetBlenderObject()) {
// In some case the game object can contains a nullptr blender object e.g default camera.
m_logicmgr->UnregisterGameObj(gameobj->GetBlenderObject(), gameobj);
}
// Remove all sensors/controllers/actuators from logicsystem.
SCA_SensorList& sensors = gameobj->GetSensors();
for (SCA_ISensor *sensor : sensors) {
m_logicmgr->RemoveSensor(sensor);
}
SCA_ControllerList& controllers = gameobj->GetControllers();
for (SCA_IController *controller : controllers) {
m_logicmgr->RemoveController(controller);
controller->ReParent(nullptr);
}
SCA_ActuatorList& actuators = gameobj->GetActuators();
for (SCA_IActuator *actuator : actuators) {
m_logicmgr->RemoveActuator(actuator);
}
// The sensors/controllers/actuators must also be released, this is done in ~SCA_IObject.
// Now remove the timer properties from the time manager.