-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphprop.cpp
1226 lines (979 loc) · 35 KB
/
phprop.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
/*
@Copyright Looking Glass Studios, Inc.
1996,1997,1998,1999,2000 Unpublished Work.
*/
///////////////////////////////////////////////////////////////////////////////
// $Header: r:/t2repos/thief2/src/physics/phprop.cpp,v 1.72 2000/03/07 19:56:19 toml Exp $
//
// Physics model properties
//
#include <property.h>
#include <propbase.h>
#include <propface.h>
#include <propert_.h>
#include <dataops_.h>
#include <prophash.h>
#include <sdesbase.h>
#include <sdesc.h>
#include <simpwrap.h>
#include <posprop.h>
#include <objpos.h>
#include <objshape.h>
#include <objscale.h>
#include <mnumprop.h>
#include <relation.h>
#include <linkbase.h>
#include <physapi.h>
#include <phmods.h>
#include <phmod.h>
#include <phdyn.h>
#include <phcore.h>
#include <phmodobb.h>
#include <phmodsph.h>
#include <vernum.h>
#include <tagfile.h>
#include <objnotif.h>
#include <phprop.h>
#include <mprintf.h>
// Must be last header
#include <dbmem.h>
BOOL PhysicsListenerLock;
////////////////////////////////////////////////////////////////////////////////
void LGAPI PhysTypeListener(sPropertyListenMsg *msg, PropListenerData data);
void LGAPI PhysAttrListener(sPropertyListenMsg *msg, PropListenerData data);
void LGAPI PhysStateListener(sPropertyListenMsg *msg, PropListenerData data);
void LGAPI PhysControlListener(sPropertyListenMsg *msg, PropListenerData data);
void LGAPI PhysDimsListener(sPropertyListenMsg *msg, PropListenerData data);
void PhysAuxListener(sPropertyListenMsg *msg, PropListenerData data, int type);
#ifdef EDITOR
extern void CreateAuxPhysPropEditor(IProperty *prop, int type);
#else
#define CreateAuxPhysPropEditor(x, y)
#endif
IPhysTypeProperty *g_pPhysTypeProp = NULL;
IPhysAttrProperty *g_pPhysAttrProp = NULL;
IPhysStateProperty *g_pPhysStateProp = NULL;
IPhysControlProperty *g_pPhysControlProp = NULL;
IPhysDimsProperty *g_pPhysDimsProp = NULL;
IBoolProperty *g_pPhysCanMantleProp = NULL;
//////////////////////////////////////////////////
// physics system tag file structures
TagFileTag PhysicsSystemTag = { "PHYS_SYSTEM" };
TagVersion PhysicsSystemVersion = { 0, 1 };
static ITagFile* tagfile = NULL;
static void movefunc(void *buf, size_t elsize, size_t nelem)
{
ITagFile_Move(tagfile,(char*)buf,elsize*nelem);
}
static BOOL setup_tagfile(ITagFile* file, TagFileTag *tag,
TagVersion *version)
{
HRESULT result;
TagVersion found_version = *version;
tagfile = file;
result = ITagFile_OpenBlock(file, tag, &found_version);
return (result == S_OK
&& found_version.major == version->major
&& found_version.minor == version->minor);
}
static void cleanup_tagfile(ITagFile* file)
{
ITagFile_CloseBlock(file);
}
////////////////////////////////////////
static sPropertyConstraint phys_type_constraints[] =
{
{ kPropertyRequires, PROP_POSITION_NAME },
{ kPropertyNullConstraint }
};
static sPropertyDesc PhysTypePropDesc =
{
PHYS_TYPE_NAME,
kPropertyNoInherit | kPropertyInstantiate,
phys_type_constraints,
1, // version
0, // last ok version (0 means no other ok versions)
{ "Physics: Model", "Type"},
kPropertyChangeLocally, // net_flags
};
class cPhysTypeOps : public cClassDataOps<cPhysTypeProp>
{
public:
cPhysTypeOps() : cClassDataOps<cPhysTypeProp>(kNoFlags) {};
STDMETHOD_(int, Version)() { return 1002; };
STDMETHOD(Read)(sDatum* pdat, IDataOpsFile* file, int version);
};
STDMETHODIMP cPhysTypeOps::Read(sDatum* pdat, IDataOpsFile* file, int version)
{
if (!pdat->value)
*pdat = New();
sPhysTypeProp *pTypeProp = (sPhysTypeProp *)pdat->value;
file->Read(&pTypeProp->type, sizeof(eModelType));
file->Read(&pTypeProp->num_submodels, sizeof(int));
if (version < 1001)
{
BOOL dummy;
file->Read(&dummy, sizeof(BOOL));
}
if (version >= 1000)
file->Read(&pTypeProp->remove_on_sleep, sizeof(BOOL));
// fixup num submodels
if (version < 1000)
{
switch (pTypeProp->type)
{
case kOBBProp: pTypeProp->num_submodels = 6; break;
case kSphereHatProp: pTypeProp->num_submodels = 2; break;
}
}
pTypeProp->remove_on_sleep = !!(pTypeProp->remove_on_sleep);
if (version < 1002)
pTypeProp->special = FALSE;
else
file->Read(&pTypeProp->special, sizeof(BOOL));
return S_OK;
}
typedef cHashPropertyStore<cPhysTypeOps> cPhysTypeStore;
typedef cSpecificProperty<IPhysTypeProperty, &IID_IPhysTypeProperty, cPhysTypeProp *, cPhysTypeStore> cBasePhysTypeProperty;
class cPhysTypeProperty : public cBasePhysTypeProperty
{
public:
cPhysTypeProperty(const sPropertyDesc* desc) : cBasePhysTypeProperty(desc) {};
STDMETHOD_(void,Notify)(ePropertyNotifyMsg msg, PropNotifyData data)
{
uObjNotifyData objdata;
objdata.raw = data;
switch (NOTIFY_MSG(msg))
{
case kObjNotifyLoad:
if (msg & kObjPartConcrete)
{
if (setup_tagfile(objdata.db.load, &PhysicsSystemTag,
&PhysicsSystemVersion))
{
PhysRead(movefunc,DB_OBJ_PARTITION(msg));
cleanup_tagfile(objdata.db.load);
}
}
break;
case kObjNotifySave:
if (msg & kObjPartConcrete)
if (setup_tagfile(objdata.db.save, &PhysicsSystemTag,
&PhysicsSystemVersion))
{
PhysWrite(movefunc, DB_OBJ_PARTITION(msg));
cleanup_tagfile(objdata.db.save);
}
break;
}
cBasePhysTypeProperty::Notify(msg,data);
}
STANDARD_DESCRIBE_TYPE(cPhysTypeProp);
};
////////////////////////////////////////
static sPropertyConstraint phys_attr_constraints[] =
{
{ kPropertyRequires, PHYS_TYPE_NAME },
{ kPropertyNullConstraint }
};
static sPropertyDesc PhysAttrPropDesc =
{
PHYS_ATTR_NAME,
kPropertyNoInherit | kPropertyInstantiate,
phys_attr_constraints,
2, // version
1, // last ok version (0 means no other ok versions)
{ "Physics: Model", "Attributes"},
kPropertyChangeLocally, // net_flags
};
class cPhysAttrOps : public cClassDataOps<cPhysAttrProp>
{
public:
cPhysAttrOps() : cClassDataOps<cPhysAttrProp>(kNoFlags) {};
STDMETHOD_(int,Version)() { return 1001; };
STDMETHOD(Read)(sDatum* pdat, IDataOpsFile* file, int version);
};
STDMETHODIMP cPhysAttrOps::Read(sDatum* pdat, IDataOpsFile* file, int version)
{
if (!pdat->value)
*pdat = New();
sPhysAttrProp *pAttrProp = (sPhysAttrProp *)pdat->value;
file->Read(&pAttrProp->gravity, sizeof(mxs_real));
file->Read(&pAttrProp->mass, sizeof(mxs_real));
file->Read(&pAttrProp->density, sizeof(mxs_real));
file->Read(&pAttrProp->elasticity, sizeof(mxs_real));
file->Read(&pAttrProp->base_friction, sizeof(mxs_real));
file->Read(&pAttrProp->cog_offset, sizeof(mxs_vector));
file->Read(&pAttrProp->rot_axes, sizeof(int));
file->Read(&pAttrProp->rest_axes, sizeof(int));
file->Read(&pAttrProp->climbable_sides, sizeof(int));
file->Read(&pAttrProp->edge_trigger, sizeof(BOOL));
if (version >= 1001)
file->Read(&pAttrProp->pore_size, sizeof(mxs_real));
else
pAttrProp->pore_size = 0.0;
pAttrProp->edge_trigger = !!(pAttrProp->edge_trigger);
return S_OK;
}
typedef cHashPropertyStore<cPhysAttrOps> cPhysAttrStore;
typedef cSpecificProperty<IPhysAttrProperty, &IID_IPhysAttrProperty, cPhysAttrProp *, cPhysAttrStore> cBasePhysAttrProperty;
class cPhysAttrProperty : public cBasePhysAttrProperty
{
public:
cPhysAttrProperty(const sPropertyDesc* desc) : cBasePhysAttrProperty(desc) {};
STANDARD_DESCRIBE_TYPE(cPhysAttrProp);
protected:
virtual void CreateEditor()
{
CreateAuxPhysPropEditor(this, PHYS_ATTR);
}
};
////////////////////////////////////////
static sPropertyConstraint phys_state_constraints[] =
{
{ kPropertyRequires, PHYS_TYPE_NAME },
{ kPropertyNullConstraint }
};
static sPropertyDesc PhysStatePropDesc =
{
PHYS_STATE_NAME,
kPropertyNoInherit | kPropertyInstantiate,
phys_state_constraints,
1, // version
0, // last ok version (0 means no other ok versions)
{ "Physics: Model", "State"},
kPropertyChangeLocally, // net_flags
};
class cPhysStateOps : public cClassDataOps<cPhysStateProp>
{
public:
cPhysStateOps() : cClassDataOps<cPhysStateProp>(kNoFlags) {};
};
typedef cHashPropertyStore<cPhysStateOps> cPhysStateStore;
typedef cSpecificProperty<IPhysStateProperty, &IID_IPhysStateProperty, cPhysStateProp *, cPhysStateStore> cBasePhysStateProperty;
class cPhysStateProperty : public cBasePhysStateProperty
{
public:
cPhysStateProperty(const sPropertyDesc* desc) : cBasePhysStateProperty(desc) {};
STANDARD_DESCRIBE_TYPE(cPhysStateProp);
protected:
virtual void CreateEditor()
{
CreateAuxPhysPropEditor(this, PHYS_STATE);
}
};
////////////////////////////////////////
static sPropertyConstraint phys_control_constraints[] =
{
{ kPropertyRequires, PHYS_TYPE_NAME },
{ kPropertyNullConstraint }
};
static sPropertyDesc PhysControlPropDesc =
{
PHYS_CONTROL_NAME,
kPropertyNoInherit | kPropertyInstantiate,
phys_control_constraints,
1, // version
0, // last ok version (0 means no other ok versions)
{ "Physics: Model", "Controls"},
kPropertyChangeLocally, // net_flags
};
class cPhysControlOps : public cClassDataOps<cPhysControlProp>
{
public:
cPhysControlOps() : cClassDataOps<cPhysControlProp>(kNoFlags) {};
STDMETHOD_(int,Version)() { return 1000; };
STDMETHOD(Read)(sDatum* pdat, IDataOpsFile* file, int version);
};
STDMETHODIMP cPhysControlOps::Read(sDatum* pdat, IDataOpsFile* file, int version)
{
if (!pdat->value)
*pdat = New();
sPhysControlProp *pControlProp = (sPhysControlProp *)pdat->value;
if (version < 1000)
{
BOOL state;
pControlProp->control_prop_types = 0;
file->Read(&state, sizeof(BOOL));
if (state) pControlProp->control_prop_types |= kCPT_AxisVelocity;
file->Read(&pControlProp->axis_vel, sizeof(mxs_vector));
file->Read(&state, sizeof(BOOL));
if (state) pControlProp->control_prop_types |= kCPT_Velocity;
file->Read(&pControlProp->vel, sizeof(mxs_vector));
file->Read(&state, sizeof(BOOL));
if (state) pControlProp->control_prop_types |= kCPT_RotVelocity;
file->Read(&pControlProp->rot_vel, sizeof(mxs_vector));
file->Read(&state, sizeof(BOOL));
if (state) pControlProp->control_prop_types |= kCPT_Location;
file->Read(&state, sizeof(BOOL));
if (state) pControlProp->control_prop_types |= kCPT_Rotation;
}
else
{
file->Read(&pControlProp->control_prop_types, sizeof(int));
file->Read(&pControlProp->axis_vel, sizeof(mxs_vector));
file->Read(&pControlProp->vel, sizeof(mxs_vector));
file->Read(&pControlProp->rot_vel, sizeof(mxs_vector));
}
return S_OK;
}
typedef cHashPropertyStore<cPhysControlOps> cPhysControlStore;
typedef cSpecificProperty<IPhysControlProperty, &IID_IPhysControlProperty, cPhysControlProp *, cPhysControlStore> cBasePhysControlProperty;
class cPhysControlProperty : public cBasePhysControlProperty
{
public:
cPhysControlProperty(const sPropertyDesc* desc) : cBasePhysControlProperty(desc) {};
STANDARD_DESCRIBE_TYPE(cPhysControlProp);
protected:
virtual void CreateEditor()
{
CreateAuxPhysPropEditor(this, PHYS_CONTROL);
}
};
////////////////////////////////////////
static sPropertyConstraint phys_dims_constraints[] =
{
{ kPropertyRequires, PHYS_TYPE_NAME },
{ kPropertyNullConstraint }
};
static sPropertyDesc PhysDimsPropDesc =
{
PHYS_DIMS_NAME,
kPropertyNoInherit | kPropertyInstantiate,
phys_dims_constraints,
1, // version
0, // last ok version (0 means no other ok versions)
{ "Physics: Model", "Dimensions"},
kPropertyChangeLocally, // net_flags
};
class cPhysDimsOps : public cClassDataOps<cPhysDimsProp>
{
public:
cPhysDimsOps() : cClassDataOps<cPhysDimsProp>(kNoFlags) {};
STDMETHOD_(int,Version)() { return 1001; };
STDMETHOD(Read)(sDatum* pdat, IDataOpsFile* file, int version);
};
STDMETHODIMP cPhysDimsOps::Read(sDatum* pdat, IDataOpsFile* file, int version)
{
if (!pdat->value)
*pdat = New();
sPhysDimsProp *pDimsProp = (sPhysDimsProp *)pdat->value;
file->Read(&pDimsProp->radius, sizeof(mxs_real) * MAX_PROP_SUBMODELS);
file->Read(&pDimsProp->offset, sizeof(mxs_vector) * MAX_PROP_SUBMODELS);
file->Read(&pDimsProp->size, sizeof(mxs_vector));
file->Read(&pDimsProp->pt_vs_terrain, sizeof(BOOL));
if (version < 1001)
pDimsProp->pt_vs_not_special = FALSE;
else
file->Read(&pDimsProp->pt_vs_not_special, sizeof(BOOL));
return S_OK;
}
typedef cHashPropertyStore<cPhysDimsOps> cPhysDimsStore;
typedef cSpecificProperty<IPhysDimsProperty, &IID_IPhysDimsProperty, cPhysDimsProp *, cPhysDimsStore> cBasePhysDimsProperty;
class cPhysDimsProperty : public cBasePhysDimsProperty
{
public:
cPhysDimsProperty(const sPropertyDesc* desc) : cBasePhysDimsProperty(desc) {};
STANDARD_DESCRIBE_TYPE(cPhysDimsProp);
protected:
virtual void CreateEditor()
{
CreateAuxPhysPropEditor(this, PHYS_DIMS);
}
};
////////////////////////////////////////
static sPropertyDesc _g_PhysCanMantleProp =
{
PHYS_CANMANTLE_NAME,
0,
NULL, 0, 0,
{ "Physics: Model", "Mantleable" },
kPropertyChangeLocally,
};
////////////////////////////////////////
// conveyor belt velocity (offset to control velocity for AIs and player)
IVectorProperty *g_pPhysConveyorVelProp = NULL;
static sPropertyDesc ConveyorVelDesc =
{ "ConveyorVel",
0,
NULL, 0, 0,
{ "Physics: Model", "ConveyorVelocity" },
kPropertyChangeLocally
};
////////////////////////////////////////
void InitPhysicsProperties()
{
g_pPhysTypeProp = new cPhysTypeProperty(&PhysTypePropDesc);
g_pPhysAttrProp = new cPhysAttrProperty(&PhysAttrPropDesc);
g_pPhysStateProp = new cPhysStateProperty(&PhysStatePropDesc);
g_pPhysControlProp = new cPhysControlProperty(&PhysControlPropDesc);
g_pPhysDimsProp = new cPhysDimsProperty(&PhysDimsPropDesc);
g_pPhysCanMantleProp = CreateBoolProperty(&_g_PhysCanMantleProp, kPropertyImplHash);
g_pPhysTypeProp->Listen(kListenPropModify | kListenPropSet | kListenPropUnset, PhysTypeListener, NULL);
g_pPhysAttrProp->Listen(kListenPropModify | kListenPropSet | kListenPropUnset, PhysAttrListener, NULL);
g_pPhysStateProp->Listen(kListenPropModify | kListenPropSet | kListenPropUnset, PhysStateListener, NULL);
g_pPhysControlProp->Listen(kListenPropModify | kListenPropSet | kListenPropUnset, PhysControlListener, NULL);
g_pPhysDimsProp->Listen(kListenPropModify | kListenPropSet | kListenPropUnset, PhysDimsListener, NULL);
// Register type sdesc
AutoAppIPtr_(StructDescTools, pSDT);
pSDT->Register(GetPhysTypeDesc());
// Register default other sdescs (for property getting/setting froms scripts)
pSDT->Register(GetPhysAttrDesc());
pSDT->Register(GetPhysStateDesc());
pSDT->Register(GetPhysControlDesc());
pSDT->Register(GetPhysDimsDesc());
// create and register conveyor velocity property
g_pPhysConveyorVelProp = CreateVectorProperty( &ConveyorVelDesc, kPropertyImplSparse );
}
void TermPhysicsProperties()
{
SafeRelease(g_pPhysTypeProp);
SafeRelease(g_pPhysAttrProp);
SafeRelease(g_pPhysStateProp);
SafeRelease(g_pPhysControlProp);
SafeRelease(g_pPhysDimsProp);
SafeRelease(g_pPhysCanMantleProp);
SafeRelease(g_pPhysConveyorVelProp);
}
////////////////////////////////////////////////////////////////////////////////
BOOL PhysListenerLock = FALSE;
void LGAPI PhysTypeListener(sPropertyListenMsg * msg, PropListenerData data)
{
if (PhysListenerLock)
return;
// Delete
if (msg->type & kListenPropUnset)
{
g_pPhysAttrProp->Delete(msg->obj);
g_pPhysStateProp->Delete(msg->obj);
g_pPhysControlProp->Delete(msg->obj);
g_pPhysDimsProp->Delete(msg->obj);
PhysDeregisterModel(msg->obj);
return;
}
// Force a model load
if (OBJ_IS_CONCRETE(msg->obj))
ObjLoadModel(msg->obj);
cPhysModel *pModel;
ePhysModelType new_type;
BOOL rebuild = FALSE;
cPhysTypeProp typeProp((cPhysTypeProp *)msg->value.ptrval);
cPhysDimsProp *pDimsProp;
if ((pModel = g_PhysModels.Get(msg->obj)) == NULL)
rebuild = TRUE;
if (typeProp.type == kNoneProp)
{
if (OBJ_IS_CONCRETE(msg->obj))
{
g_pPhysTypeProp->Delete(msg->obj);
g_pPhysAttrProp->Delete(msg->obj);
g_pPhysStateProp->Delete(msg->obj);
g_pPhysControlProp->Delete(msg->obj);
g_pPhysDimsProp->Delete(msg->obj);
}
return;
}
// Convert sdesc enum into model type
switch (typeProp.type)
{
case kOBBProp:
{
new_type = kPMT_OBB;
if (pModel && (pModel->GetType(0) != kPMT_OBB))
rebuild = TRUE;
break;
}
case kSphereProp:
{
new_type = kPMT_Sphere;
if (pModel && ((pModel->GetType(0) != kPMT_Sphere) && (pModel->GetType(0) != kPMT_Point)))
rebuild = TRUE;
break;
}
case kSphereHatProp:
{
new_type = kPMT_SphereHat;
if (pModel && (pModel->GetType(0) != kPMT_SphereHat))
rebuild = TRUE;
break;
}
default:
Warning(("PhysTypeListener: Unknown type: %d\n", (int)typeProp.type));
return;
}
// Are we changing the number of submodels?
if (pModel && (pModel->GetType(0) != kPMT_OBB) && (pModel->NumSubModels() != typeProp.num_submodels))
rebuild = TRUE;
if (rebuild && OBJ_IS_CONCRETE(msg->obj))
{
// @NOTE: We want to make sure that the Dims property is getting
// instantiated before the type property, so that if there isn't
// a dims property at this point, then we really don't have one
// on any ancestor. Curently, we're not sure whether it's going
// to be instantiated or not.
PhysListenerLock = TRUE;
// Remove old physics
PhysDeregisterModel(msg->obj);
// Initialize the type property
switch (typeProp.type)
{
case kOBBProp: typeProp.num_submodels = 6; break;
case kSphereHatProp: typeProp.num_submodels = 2; break;
}
g_pPhysTypeProp->Set(msg->obj, &typeProp);
g_pPhysAttrProp->Create(msg->obj);
g_pPhysStateProp->Create(msg->obj);
g_pPhysControlProp->Create(msg->obj);
g_pPhysDimsProp->Create(msg->obj);
g_pPhysDimsProp->Get(msg->obj, &pDimsProp);
// Add new physics
switch (typeProp.type)
{
case kOBBProp: PhysRegisterOBB(msg->obj, kPMCF_Default);
break;
case kSphereProp: PhysRegisterSphere(msg->obj, typeProp.num_submodels, kPMCF_Default, pDimsProp->radius[0]);
break;
case kSphereHatProp: PhysRegisterSphereHat(msg->obj, kPMCF_Default, pDimsProp->radius[0]);
break;
}
PhysListenerLock = FALSE;
// Refresh physics with the property
UpdatePhysModel(msg->obj, PHYS_ATTR | PHYS_STATE | PHYS_CONTROL | PHYS_DIMS);
}
if (OBJ_IS_CONCRETE(msg->obj))
{
pModel = g_PhysModels.Get(msg->obj);
pModel->SetRemovesOnSleep(typeProp.remove_on_sleep);
pModel->SetSpecial(typeProp.special);
if (!g_pPhysAttrProp->IsRelevant(msg->obj))
g_pPhysAttrProp->Create(msg->obj);
if (!g_pPhysStateProp->IsRelevant(msg->obj))
g_pPhysStateProp->Create(msg->obj);
if (!g_pPhysControlProp->IsRelevant(msg->obj))
g_pPhysControlProp->Create(msg->obj);
if (!g_pPhysDimsProp->IsRelevant(msg->obj))
g_pPhysDimsProp->Create(msg->obj);
}
}
static BOOL PhysAuxListenerLock = FALSE;
void LGAPI PhysAttrListener(sPropertyListenMsg *msg, PropListenerData data)
{
if (!g_pPhysTypeProp->IsRelevant(msg->obj) && !(msg->type & kListenPropUnset))
{
PhysAuxListenerLock = TRUE;
g_pPhysAttrProp->Delete(msg->obj);
PhysAuxListenerLock = FALSE;
return;
}
PhysAuxListener(msg, data, PHYS_ATTR);
}
void LGAPI PhysStateListener(sPropertyListenMsg *msg, PropListenerData data)
{
if (!g_pPhysTypeProp->IsRelevant(msg->obj) && !(msg->type & kListenPropUnset))
{
PhysAuxListenerLock = TRUE;
g_pPhysStateProp->Delete(msg->obj);
PhysAuxListenerLock = FALSE;
return;
}
PhysAuxListener(msg, data, PHYS_STATE);
}
void LGAPI PhysControlListener(sPropertyListenMsg *msg, PropListenerData data)
{
if (!g_pPhysTypeProp->IsRelevant(msg->obj) && !(msg->type & kListenPropUnset))
{
PhysAuxListenerLock = TRUE;
g_pPhysControlProp->Delete(msg->obj);
PhysAuxListenerLock = FALSE;
return;
}
PhysAuxListener(msg, data, PHYS_CONTROL);
}
void LGAPI PhysDimsListener(sPropertyListenMsg *msg, PropListenerData data)
{
if (!g_pPhysTypeProp->IsRelevant(msg->obj) && !(msg->type & kListenPropUnset))
{
PhysAuxListenerLock = TRUE;
g_pPhysDimsProp->Delete(msg->obj);
PhysAuxListenerLock = FALSE;
return;
}
PhysAuxListener(msg, data, PHYS_DIMS);
}
void PhysAuxListener(sPropertyListenMsg *msg, PropListenerData data, int type)
{
if (PhysAuxListenerLock)
return;
BOOL has_physics = PhysObjHasPhysics(msg->obj);
// Created w/o archetype
if ((msg->type & kListenPropSet) && !(msg->type & kListenPropModify))
{
if (has_physics)
UpdatePhysProperty(msg->obj, type);
else
InitPhysProperty(msg->obj, type);
}
if ((msg->type & kListenPropModify) && has_physics)
{
UpdatePhysModel(msg->obj, type);
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Initialize instance-specific portions of the physics property that are
// outside of physics's domain. (location, facing, size, etc)
//
void InitPhysProperty(ObjID objID, int type)
{
cPhysTypeProp *pTypeProp;
if (!g_pPhysTypeProp->Get(objID, &pTypeProp))
return;
PhysAuxListenerLock = TRUE;
if (type & PHYS_ATTR)
{
}
if (type & PHYS_STATE)
{
cPhysStateProp *pStateProp;
if (!g_pPhysStateProp->Get(objID, &pStateProp))
{
g_pPhysStateProp->Create(objID);
g_pPhysStateProp->Get(objID, &pStateProp);
}
pStateProp->location = ObjPosGet(objID)->loc.vec;
ANGVEC_TO_DEGVEC(&pStateProp->facing, ObjPosGet(objID)->fac);
g_pPhysStateProp->Set(objID, pStateProp);
}
if (type & PHYS_CONTROL)
{
}
if (type & PHYS_DIMS)
{
cPhysDimsProp *pDimsProp;
if (!g_pPhysDimsProp->Get(objID, &pDimsProp))
{
g_pPhysDimsProp->Create(objID);
g_pPhysDimsProp->Get(objID, &pDimsProp);
}
switch (pTypeProp->type)
{
case kOBBProp:
{
mxs_vector scale;
ObjGetUnscaledDims(objID, &pDimsProp->size);
if (ObjGetScale(objID, &scale))
{
pDimsProp->size.x *= scale.x;
pDimsProp->size.y *= scale.y;
pDimsProp->size.z *= scale.z;
}
break;
}
case kSphereProp:
{
mxs_vector size, scale;
ObjGetUnscaledDims(objID, &size);
if (ObjGetScale(objID, &scale))
{
size.x *= scale.x;
size.y *= scale.y;
size.z *= scale.z;
}
for (int i=0; i<pTypeProp->num_submodels && i<MAX_PROP_SUBMODELS; i++)
pDimsProp->radius[i] = size.z / 2;
break;
}
case kSphereHatProp:
{
mxs_vector size, scale;
ObjGetUnscaledDims(objID, &size);
if (ObjGetScale(objID, &scale))
{
size.x *= scale.x;
size.y *= scale.y;
size.z *= scale.z;
}
pDimsProp->radius[0] = size.z / 2;
break;
}
}
g_pPhysDimsProp->Set(objID, pDimsProp);
}
PhysAuxListenerLock = FALSE;
}
////////////////////////////////////////////////////////////////////////////////
void UpdatePhysProperty(ObjID objID, int type)
{
cPhysModel *pModel = g_PhysModels.Get(objID);
if (!pModel)
return;
PhysAuxListenerLock = TRUE;
if (type & PHYS_TYPE)
{
cPhysTypeProp *pTypeProp;
if (!g_pPhysTypeProp->Get(objID, &pTypeProp))
{
g_pPhysTypeProp->Create(objID);
g_pPhysTypeProp->Get(objID, &pTypeProp);
}
switch (pModel->GetType(0))
{
case kPMT_Sphere: case kPMT_Point:
pTypeProp->type = kSphereProp;
break;
case kPMT_OBB:
pTypeProp->type = kOBBProp;
break;
case kPMT_SphereHat:
pTypeProp->type = kSphereHatProp;
break;
case kPMT_BSP:
default:
Warning(("UpdatePhysProperty: bad or missing physics type for obj %d\n", objID));
// hell, why not...
pTypeProp->type = kSphereProp;
break;
}
pTypeProp->num_submodels = pModel->NumSubModels();
pTypeProp->remove_on_sleep = pModel->RemovesOnSleep();
pTypeProp->special = pModel->IsSpecial();
g_pPhysTypeProp->Set(objID, pTypeProp);
}
if (type & PHYS_ATTR)
{
cPhysAttrProp *pAttrProp;
if (!g_pPhysAttrProp->Get(objID, &pAttrProp))
{
g_pPhysAttrProp->Create(objID);
g_pPhysAttrProp->Get(objID, &pAttrProp);
}
pAttrProp->gravity = pModel->GetGravity() * 100;
pAttrProp->mass = pModel->GetDynamics()->GetMass();
pAttrProp->density = pModel->GetDynamics()->GetDensity();
pAttrProp->elasticity = pModel->GetDynamics()->GetElasticity();
pAttrProp->base_friction = pModel->GetBaseFriction();
pAttrProp->cog_offset = pModel->GetCOGOffset();
switch (pModel->GetType(0))
{
case kPMT_OBB:
{
pAttrProp->climbable_sides = ((cPhysOBBModel *)pModel)->GetClimbableSides();
pAttrProp->edge_trigger = ((cPhysOBBModel *)pModel)->IsEdgeTrigger();
pAttrProp->pore_size = ((cPhysOBBModel *)pModel)->GetPoreSize();
break;
}
case kPMT_Sphere:
case kPMT_Point:
case kPMT_SphereHat:
{
pAttrProp->rot_axes = pModel->GetRotAxes();
pAttrProp->rest_axes = pModel->GetRestAxes();
break;
}
}
g_pPhysAttrProp->Set(objID, pAttrProp);
}
if (type & PHYS_STATE)
{
cPhysStateProp *pStateProp;
if (!g_pPhysStateProp->Get(objID, &pStateProp))
{
g_pPhysStateProp->Create(objID);
g_pPhysStateProp->Get(objID, &pStateProp);
}
pStateProp->location = pModel->GetLocationVec();
ANGVEC_TO_DEGVEC(&pStateProp->facing, pModel->GetRotation());
pStateProp->velocity = pModel->GetVelocity();
pStateProp->rot_velocity = pModel->GetDynamics()->GetRotationalVelocity();
g_pPhysStateProp->Set(objID, pStateProp);
}
if (type & PHYS_CONTROL)
{
cPhysControlProp *pControlProp;
if (!g_pPhysControlProp->Get(objID, &pControlProp))
{
g_pPhysControlProp->Create(objID);
g_pPhysControlProp->Get(objID, &pControlProp);
}