-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelHolder2.es
834 lines (739 loc) · 27.7 KB
/
ModelHolder2.es
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
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
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. */
210
%{
#include "StdH.h"
#include "Entities/WorldSettingsController.h"
%}
uses "Entities/ModelDestruction";
uses "Entities/AnimationChanger";
uses "Entities/BloodSpray";
enum CustomShadingType {
0 CST_NONE "None",
1 CST_FULL_CUSTOMIZED "Full customized",
2 CST_CONSTANT_SHADING "Constant shading"
};
enum ShadowType {
0 ST_NONE "None",
1 ST_CLUSTER "Cluster shadows",
2 ST_POLYGONAL "Polygonal"
};
%{
#define MIPRATIO 0.003125f //(2*tan(90/2))/640
%}
class CModelHolder2 : CRationalEntity {
name "ModelHolder2";
thumbnail "Thumbnails\\ModelHolder.tbn";
features "HasName", "HasDescription";
properties:
1 CTFileName m_fnModel "Model" 'M' =CTFILENAME("Models\\Editor\\Axis.mdl"),
2 CTFileName m_fnTexture "Texture" 'T' =CTFILENAME("Models\\Editor\\Vector.tex"),
22 CTFileName m_fnReflection "Reflection" =CTString(""),
23 CTFileName m_fnSpecular "Specular" =CTString(""),
24 CTFileName m_fnBump "Bump" =CTString(""),
3 FLOAT m_fStretchAll "StretchAll" 'S' = 1.0f,
4 FLOAT m_fStretchX "StretchX" 'X' = 1.0f,
5 FLOAT m_fStretchY "StretchY" 'Y' = 1.0f,
6 FLOAT m_fStretchZ "StretchZ" 'Z' = 1.0f,
7 CTString m_strName "Name" 'N' ="",
12 CTString m_strDescription = "",
8 BOOL m_bColliding "Colliding" 'L' = FALSE, // set if model is not immatierial
9 ANIMATION m_iModelAnimation "Model animation" = 0,
10 ANIMATION m_iTextureAnimation "Texture animation" = 0,
11 enum ShadowType m_stClusterShadows "Shadows" 'W' = ST_CLUSTER, // set if model uses cluster shadows
13 BOOL m_bBackground "Background" 'B' = FALSE, // set if model is rendered in background
21 BOOL m_bTargetable "Targetable" = FALSE, // st if model should be targetable
// parameters for custom shading of a model (overrides automatic shading calculation)
14 enum CustomShadingType m_cstCustomShading "Custom shading" 'H' = CST_NONE,
15 ANGLE3D m_aShadingDirection "Light direction" 'D' = ANGLE3D( AngleDeg(45.0f),AngleDeg(45.0f),AngleDeg(45.0f)),
16 COLOR m_colLight "Light color" 'O' = C_WHITE,
17 COLOR m_colAmbient "Ambient color" 'A' = C_BLACK,
18 CTFileName m_fnmLightAnimation "Light animation file" = CTString(""),
19 ANIMATION m_iLightAnimation "Light animation" = 0,
20 CAnimObject m_aoLightAnimation,
25 BOOL m_bAttachments "Attachments" = TRUE, // set if model should auto load attachments
26 BOOL m_bActive "Active" = TRUE,
31 FLOAT m_fMipAdd "Mip Add" = 0.0f,
32 FLOAT m_fMipMul "Mip Mul" = 1.0f,
//33 FLOAT m_fMipFadeDist "Mip Fade Dist" = 0.0f,
//34 FLOAT m_fMipFadeLen "Mip Fade Len" = 0.0f,
33 FLOAT m_fMipFadeDist = 0.0f,
34 FLOAT m_fMipFadeLen = 0.0f,
35 RANGE m_rMipFadeDistMetric "Mip Fade Dist (Metric)" = -1.0f,
36 FLOAT m_fMipFadeLenMetric "Mip Fade Len (Metric)" = -1.0f,
// random values variables
50 BOOL m_bRandomStretch "Apply RND stretch" = FALSE, // apply random stretch
52 FLOAT m_fStretchRndX "Stretch RND X (%)" = 0.2f, // random stretch width
51 FLOAT m_fStretchRndY "Stretch RND Y (%)" = 0.2f, // random stretch height
53 FLOAT m_fStretchRndZ "Stretch RND Z (%)" = 0.2f, // random stretch depth
54 FLOAT m_fStretchRndAll "Stretch RND All (%)" = 0.0f, // random stretch all
55 FLOAT3D m_fStretchRandom = FLOAT3D(1, 1, 1),
// destruction values
60 CEntityPointer m_penDestruction "Destruction" 'Q' COLOR(C_BLACK|0x20), // model destruction entity
61 FLOAT3D m_vDamage = FLOAT3D(0,0,0), // current damage impact
62 FLOAT m_tmLastDamage = -1000.0f,
63 CEntityPointer m_penDestroyTarget "Destruction Target" COLOR(C_WHITE|0xFF), // targeted when destroyed
64 CEntityPointer m_penLastDamager,
65 FLOAT m_tmSpraySpawned = 0.0f, // time when damage has been applied
66 FLOAT m_fSprayDamage = 0.0f, // total ammount of damage
67 CEntityPointer m_penSpray, // the blood spray
68 FLOAT m_fMaxDamageAmmount = 0.0f, // max ammount of damage recived in in last xxx ticks
70 FLOAT m_fClassificationStretch "Classification stretch" = 1.0f, // classification box multiplier
80 COLOR m_colBurning = COLOR(C_WHITE|CT_OPAQUE), // color applied when burning
90 enum DamageType m_dmtLastDamageType=DMT_CHAINSAW,
91 FLOAT m_fChainSawCutDamage "Chain saw cut dammage" 'C' = 300.0f,
93 INDEX m_iFirstRandomAnimation "First random animation" 'R' = -1,
100 FLOAT m_fMaxTessellationLevel "Max tessellation level" = 0.0f,
{
CTFileName m_fnOldModel; // used for remembering last selected model (not saved at all)
}
components:
1 class CLASS_BLOOD_SPRAY "Classes\\BloodSpray.ecl",
functions:
void Precache(void) {
PrecacheClass(CLASS_BLOOD_SPRAY, 0);
};
/* Fill in entity statistics - for AI purposes only */
BOOL FillEntityStatistics(EntityStats *pes)
{
pes->es_strName = m_fnModel.FileName()+", "+m_fnTexture.FileName();
pes->es_ctCount = 1;
pes->es_ctAmmount = 1;
if (m_penDestruction!=NULL) {
pes->es_strName += " (destroyable)";
pes->es_fValue = GetDestruction()->m_fHealth;
pes->es_iScore = 0;
} else {
pes->es_fValue = 0;
pes->es_iScore = 0;
}
return TRUE;
}
// classification box multiplier
FLOAT3D GetClassificationBoxStretch(void)
{
return FLOAT3D( m_fClassificationStretch, m_fClassificationStretch, m_fClassificationStretch);
}
// maximum allowed tessellation level for this model (for Truform/N-Patches support)
FLOAT GetMaxTessellationLevel(void)
{
return m_fMaxTessellationLevel;
}
/* Receive damage */
void ReceiveDamage(CEntity *penInflictor, enum DamageType dmtType,
FLOAT fDamageAmmount, const FLOAT3D &vHitPoint, const FLOAT3D &vDirection)
{
FLOAT fNewDamage = fDamageAmmount;
// if not destroyable
if (m_penDestruction==NULL) {
// do nothing
return;
}
if( dmtType==DMT_BURNING)
{
UBYTE ubR, ubG, ubB, ubA;
ColorToRGBA(m_colBurning, ubR, ubG, ubB, ubA);
ubR=ClampDn(ubR-4, 32);
m_colBurning=RGBAToColor(ubR, ubR, ubR, ubA);
}
CModelDestruction *penDestruction = GetDestruction();
// adjust damage
fNewDamage *=DamageStrength(penDestruction->m_eibtBodyType, dmtType);
// if no damage
if (fNewDamage==0) {
// do nothing
return;
}
FLOAT fKickDamage = fNewDamage;
if( (dmtType == DMT_EXPLOSION) || (dmtType == DMT_IMPACT) || (dmtType == DMT_CANNONBALL_EXPLOSION) )
{
fKickDamage*=1.5f;
}
if (dmtType == DMT_CLOSERANGE) {
fKickDamage=0.0f;
}
if (dmtType == DMT_CHAINSAW) {
fKickDamage=0.0f;
}
if(dmtType == DMT_BULLET && penDestruction->m_eibtBodyType==EIBT_ROCK) {
fKickDamage=0.0f;
}
if( dmtType==DMT_BURNING)
{
fKickDamage=0.0f;
}
// get passed time since last damage
TIME tmNow = _pTimer->CurrentTick();
TIME tmDelta = tmNow-m_tmLastDamage;
m_tmLastDamage = tmNow;
// remember who damaged you
m_penLastDamager = penInflictor;
// fade damage out
if (tmDelta>=_pTimer->TickQuantum*3) {
m_vDamage=FLOAT3D(0,0,0);
}
// add new damage
FLOAT3D vDirectionFixed;
if (vDirection.ManhattanNorm()>0.5f) {
vDirectionFixed = vDirection;
} else {
vDirectionFixed = FLOAT3D(0,1,0);
}
FLOAT3D vDamageOld = m_vDamage;
m_vDamage += vDirectionFixed*fKickDamage;
// NOTE: we don't receive damage here, but handle death differently
if (m_vDamage.Length()>GetHealth()) {
if (!penDestruction->m_bRequireExplosion ||
dmtType==DMT_EXPLOSION || dmtType==DMT_CANNONBALL || dmtType==DMT_CANNONBALL_EXPLOSION)
{
EDeath eDeath; // we don't need any extra parameters
SendEvent(eDeath);
//remember last dammage type
m_dmtLastDamageType=dmtType;
}
}
if( m_fMaxDamageAmmount<fDamageAmmount) {
m_fMaxDamageAmmount = fDamageAmmount;
}
// if it has no spray, or if this damage overflows it
if( (dmtType!=DMT_BURNING) && (dmtType!=DMT_CHAINSAW) &&
(m_tmSpraySpawned<=_pTimer->CurrentTick()-_pTimer->TickQuantum*8 ||
m_fSprayDamage+fNewDamage>50.0f))
{
// spawn blood spray
CPlacement3D plSpray = CPlacement3D( vHitPoint, ANGLE3D(0, 0, 0));
m_penSpray = CreateEntity( plSpray, CLASS_BLOOD_SPRAY);
m_penSpray->SetParent( this);
ESpawnSpray eSpawnSpray;
// adjust spray power
if( m_fMaxDamageAmmount > 10.0f) {
eSpawnSpray.fDamagePower = 3.0f;
} else if(m_fSprayDamage+fNewDamage>50.0f) {
eSpawnSpray.fDamagePower = 2.0f;
} else {
eSpawnSpray.fDamagePower = 1.0f;
}
eSpawnSpray.sptType = penDestruction->m_sptType;
eSpawnSpray.fSizeMultiplier = penDestruction->m_fParticleSize;
// get your down vector (simulates gravity)
FLOAT3D vDn(-en_mRotation(1,2), -en_mRotation(2,2), -en_mRotation(3,2));
// setup direction of spray
FLOAT3D vHitPointRelative = vHitPoint - GetPlacement().pl_PositionVector;
FLOAT3D vReflectingNormal;
GetNormalComponent( vHitPointRelative, vDn, vReflectingNormal);
vReflectingNormal.Normalize();
FLOAT3D vProjectedComponent = vReflectingNormal*(vDirection%vReflectingNormal);
FLOAT3D vSpilDirection = vDirection-vProjectedComponent*2.0f-vDn*0.5f;
eSpawnSpray.vDirection = vSpilDirection;
eSpawnSpray.penOwner = this;
eSpawnSpray.colCentralColor=penDestruction->m_colParticles;
eSpawnSpray.colBurnColor=m_colBurning;
eSpawnSpray.fLaunchPower=penDestruction->m_fParticleLaunchPower;
// initialize spray
m_penSpray->Initialize( eSpawnSpray);
m_tmSpraySpawned = _pTimer->CurrentTick();
m_fSprayDamage = 0.0f;
m_fMaxDamageAmmount = 0.0f;
}
if( dmtType==DMT_CHAINSAW && m_fChainSawCutDamage>0)
{
m_fChainSawCutDamage-=fDamageAmmount;
if(m_fChainSawCutDamage<=0)
{
EDeath eDeath; // we don't need any extra parameters
SendEvent(eDeath);
//remember last dammage type
m_dmtLastDamageType=dmtType;
}
}
m_fSprayDamage+=fNewDamage;
};
// Entity info
void *GetEntityInfo(void) {
CModelDestruction *pmd=GetDestruction();
if( pmd!=NULL)
{
return GetStdEntityInfo(pmd->m_eibtBodyType);
}
return CEntity::GetEntityInfo();
};
class CModelDestruction *GetDestruction(void)
{
ASSERT(m_penDestruction==NULL || IsOfClass(m_penDestruction, "ModelDestruction"));
return (CModelDestruction*)&*m_penDestruction;
}
BOOL IsTargetable(void) const
{
return m_bTargetable;
}
/* Get anim data for given animation property - return NULL for none. */
CAnimData *GetAnimData(SLONG slPropertyOffset)
{
if (slPropertyOffset==offsetof(CModelHolder2, m_iModelAnimation)) {
return GetModelObject()->GetData();
} else if (slPropertyOffset==offsetof(CModelHolder2, m_iTextureAnimation)) {
return GetModelObject()->mo_toTexture.GetData();
} else if (slPropertyOffset==offsetof(CModelHolder2, m_iLightAnimation)) {
return m_aoLightAnimation.GetData();
} else {
return CEntity::GetAnimData(slPropertyOffset);
}
};
/* Adjust model mip factor if needed. */
void AdjustMipFactor(FLOAT &fMipFactor)
{
// if should fade last mip
if (m_fMipFadeDist>0) {
CModelObject *pmo = GetModelObject();
if(pmo==NULL) {
return;
}
// adjust for stretch
FLOAT fMipForFade = fMipFactor;
// TODO: comment the next 3 lines for mip factors conversion
/*if (pmo->mo_Stretch != FLOAT3D(1,1,1)) {
fMipForFade -= Log2( Max(pmo->mo_Stretch(1),Max(pmo->mo_Stretch(2),pmo->mo_Stretch(3))));
}*/
// if not visible
if (fMipForFade>m_fMipFadeDist) {
// set mip factor so that model is never rendered
fMipFactor = UpperLimit(0.0f);
return;
}
// adjust fading
FLOAT fFade = (m_fMipFadeDist-fMipForFade);
if (m_fMipFadeLen>0) {
fFade/=m_fMipFadeLen;
} else {
if (fFade>0) {
fFade = 1.0f;
}
}
fFade = Clamp(fFade, 0.0f, 1.0f);
// make it invisible
pmo->mo_colBlendColor = (pmo->mo_colBlendColor&~255)|UBYTE(255*fFade);
}
fMipFactor = fMipFactor*m_fMipMul+m_fMipAdd;
}
/* Adjust model shading parameters if needed. */
BOOL AdjustShadingParameters(FLOAT3D &vLightDirection, COLOR &colLight, COLOR &colAmbient)
{
switch( m_cstCustomShading)
{
case CST_FULL_CUSTOMIZED:
{
// if there is color animation
if (m_aoLightAnimation.GetData()!=NULL) {
// get lerping info
SLONG colFrame0, colFrame1; FLOAT fRatio;
m_aoLightAnimation.GetFrame( colFrame0, colFrame1, fRatio);
UBYTE ubAnimR0, ubAnimG0, ubAnimB0;
UBYTE ubAnimR1, ubAnimG1, ubAnimB1;
ColorToRGB( colFrame0, ubAnimR0, ubAnimG0, ubAnimB0);
ColorToRGB( colFrame1, ubAnimR1, ubAnimG1, ubAnimB1);
// calculate current animation color
FLOAT fAnimR = NormByteToFloat( Lerp( ubAnimR0, ubAnimR1, fRatio));
FLOAT fAnimG = NormByteToFloat( Lerp( ubAnimG0, ubAnimG1, fRatio));
FLOAT fAnimB = NormByteToFloat( Lerp( ubAnimB0, ubAnimB1, fRatio));
// decompose constant colors
UBYTE ubLightR, ubLightG, ubLightB;
UBYTE ubAmbientR, ubAmbientG, ubAmbientB;
ColorToRGB( m_colLight, ubLightR, ubLightG, ubLightB);
ColorToRGB( m_colAmbient, ubAmbientR, ubAmbientG, ubAmbientB);
colLight = RGBToColor( ubLightR *fAnimR, ubLightG *fAnimG, ubLightB *fAnimB);
colAmbient = RGBToColor( ubAmbientR*fAnimR, ubAmbientG*fAnimG, ubAmbientB*fAnimB);
// if there is no color animation
} else {
colLight = m_colLight;
colAmbient = m_colAmbient;
}
// obtain world settings controller
CWorldSettingsController *pwsc = GetWSC(this);
if( pwsc!=NULL && pwsc->m_bApplyShadingToModels)
{
// apply animating shading
COLOR colShade = GetWorld()->wo_atbTextureBlendings[9].tb_colMultiply;
colLight=MulColors(colLight, colShade);
colAmbient=MulColors(colAmbient, colShade);
}
AnglesToDirectionVector(m_aShadingDirection, vLightDirection);
vLightDirection = -vLightDirection;
break;
}
case CST_CONSTANT_SHADING:
{
// combine colors with clamp
UBYTE lR,lG,lB,aR,aG,aB,rR,rG,rB;
ColorToRGB( colLight, lR, lG, lB);
ColorToRGB( colAmbient, aR, aG, aB);
colLight = 0;
rR = (UBYTE) Clamp( (ULONG)lR+aR, (ULONG)0, (ULONG)255);
rG = (UBYTE) Clamp( (ULONG)lG+aG, (ULONG)0, (ULONG)255);
rB = (UBYTE) Clamp( (ULONG)lB+aB, (ULONG)0, (ULONG)255);
colAmbient = RGBToColor( rR, rG, rB);
break;
}
case CST_NONE:
{
// do nothing
break;
}
}
if(m_colBurning!=COLOR(C_WHITE|CT_OPAQUE))
{
colAmbient = MulColors( colAmbient, m_colBurning);
colLight = MulColors( colLight, m_colBurning);
return TRUE;
}
return m_stClusterShadows!=ST_NONE;
};
// apply mirror and stretch to the entity
void MirrorAndStretch(FLOAT fStretch, BOOL bMirrorX)
{
m_fStretchAll*=fStretch;
if (bMirrorX) {
m_fStretchX = -m_fStretchX;
}
}
// Stretch model
void StretchModel(void) {
// stretch factors must not have extreme values
if (Abs(m_fStretchX) < 0.01f) { m_fStretchX = 0.01f; }
if (Abs(m_fStretchY) < 0.01f) { m_fStretchY = 0.01f; }
if (Abs(m_fStretchZ) < 0.01f) { m_fStretchZ = 0.01f; }
if (m_fStretchAll< 0.01f) { m_fStretchAll = 0.01f; }
if (Abs(m_fStretchX) >1000.0f) { m_fStretchX = 1000.0f*Sgn(m_fStretchX); }
if (Abs(m_fStretchY) >1000.0f) { m_fStretchY = 1000.0f*Sgn(m_fStretchY); }
if (Abs(m_fStretchZ) >1000.0f) { m_fStretchZ = 1000.0f*Sgn(m_fStretchZ); }
if (m_fStretchAll>1000.0f) { m_fStretchAll = 1000.0f; }
if (m_bRandomStretch) {
m_bRandomStretch = FALSE;
// stretch
m_fStretchRndX = Clamp( m_fStretchRndX , 0.0f, 1.0f);
m_fStretchRndY = Clamp( m_fStretchRndY , 0.0f, 1.0f);
m_fStretchRndZ = Clamp( m_fStretchRndZ , 0.0f, 1.0f);
m_fStretchRndAll = Clamp( m_fStretchRndAll , 0.0f, 1.0f);
m_fStretchRandom(1) = (FRnd()*m_fStretchRndX*2 - m_fStretchRndX) + 1;
m_fStretchRandom(2) = (FRnd()*m_fStretchRndY*2 - m_fStretchRndY) + 1;
m_fStretchRandom(3) = (FRnd()*m_fStretchRndZ*2 - m_fStretchRndZ) + 1;
FLOAT fRNDAll = (FRnd()*m_fStretchRndAll*2 - m_fStretchRndAll) + 1;
m_fStretchRandom(1) *= fRNDAll;
m_fStretchRandom(2) *= fRNDAll;
m_fStretchRandom(3) *= fRNDAll;
}
GetModelObject()->StretchModel( FLOAT3D(
m_fStretchAll*m_fStretchX*m_fStretchRandom(1),
m_fStretchAll*m_fStretchY*m_fStretchRandom(2),
m_fStretchAll*m_fStretchZ*m_fStretchRandom(3)) );
ModelChangeNotify();
};
/* Init model holder*/
void InitModelHolder(void) {
// must not crash when model is removed
if (m_fnModel=="") {
m_fnModel=CTFILENAME("Models\\Editor\\Axis.mdl");
}
if( m_fnReflection == CTString("Models\\Editor\\Vector.tex")) {
m_fnReflection = CTString("");
}
if( m_fnSpecular == CTString("Models\\Editor\\Vector.tex")) {
m_fnSpecular = CTString("");
}
if( m_fnBump == CTString("Models\\Editor\\Vector.tex")) {
m_fnBump = CTString("");
}
if (m_bActive) {
InitAsModel();
} else {
InitAsEditorModel();
}
// set appearance
SetModel(m_fnModel);
INDEX iAnim=m_iModelAnimation;
FLOAT tmOffsetPhase=0.0f;
if(m_iFirstRandomAnimation>=0)
{
INDEX ctAnims=GetModelObject()->GetAnimsCt()-m_iFirstRandomAnimation;
iAnim=m_iFirstRandomAnimation+Clamp(INDEX(FRnd()*ctAnims), INDEX(0), ctAnims);
tmOffsetPhase=FRnd()*10.0f;
}
GetModelObject()->PlayAnim(iAnim, AOF_LOOPING);
GetModelObject()->OffsetPhase(tmOffsetPhase);
// if initialized for the first time
if (m_fnOldModel=="") {
// just remember the model filename
m_fnOldModel = m_fnModel;
// if re-initialized
} else {
// if the model filename has changed
if (m_fnOldModel != m_fnModel) {
m_fnOldModel = m_fnModel;
GetModelObject()->AutoSetTextures();
m_fnTexture = GetModelObject()->mo_toTexture.GetName();
m_fnReflection = GetModelObject()->mo_toReflection.GetName();
m_fnSpecular = GetModelObject()->mo_toSpecular.GetName();
m_fnBump = GetModelObject()->mo_toBump.GetName();
}
}
if( m_bAttachments)
{
GetModelObject()->AutoSetAttachments();
}
else
{
GetModelObject()->RemoveAllAttachmentModels();
}
try
{
GetModelObject()->mo_toTexture.SetData_t(m_fnTexture);
GetModelObject()->mo_toTexture.PlayAnim(m_iTextureAnimation, AOF_LOOPING);
GetModelObject()->mo_toReflection.SetData_t(m_fnReflection);
GetModelObject()->mo_toSpecular.SetData_t(m_fnSpecular);
GetModelObject()->mo_toBump.SetData_t(m_fnBump);
} catch (char *strError) {
WarningMessage(strError);
}
// set model stretch
StretchModel();
ModelChangeNotify();
if (m_bColliding&&m_bActive) {
SetPhysicsFlags(EPF_MODEL_FIXED);
SetCollisionFlags(ECF_MODEL_HOLDER);
} else {
SetPhysicsFlags(EPF_MODEL_IMMATERIAL);
SetCollisionFlags(ECF_IMMATERIAL);
}
switch(m_stClusterShadows) {
case ST_NONE:
{
SetFlags(GetFlags()&~ENF_CLUSTERSHADOWS);
//SetFlags(GetFlags()&~ENF_POLYGONALSHADOWS);
break;
}
case ST_CLUSTER:
{
SetFlags(GetFlags()|ENF_CLUSTERSHADOWS);
//SetFlags(GetFlags()&~ENF_POLYGONALSHADOWS);
break;
}
case ST_POLYGONAL:
{
//SetFlags(GetFlags()|ENF_POLYGONALSHADOWS);
SetFlags(GetFlags()&~ENF_CLUSTERSHADOWS);
break;
}
}
if (m_bBackground) {
SetFlags(GetFlags()|ENF_BACKGROUND);
} else {
SetFlags(GetFlags()&~ENF_BACKGROUND);
}
try {
m_aoLightAnimation.SetData_t(m_fnmLightAnimation);
} catch (char *strError) {
WarningMessage(TRANS("Cannot load '%s': %s"), (CTString&)m_fnmLightAnimation, strError);
m_fnmLightAnimation = "";
}
if (m_aoLightAnimation.GetData()!=NULL) {
m_aoLightAnimation.PlayAnim(m_iLightAnimation, AOF_LOOPING);
}
if (m_penDestruction==NULL) {
m_strDescription.PrintF("%s,%s undestroyable", (CTString&)m_fnModel.FileName(), (CTString&)m_fnTexture.FileName());
} else {
m_strDescription.PrintF("%s,%s -> %s", (CTString&)m_fnModel.FileName(), (CTString&)m_fnTexture.FileName(),
m_penDestruction->GetName());
}
return;
}
// returns bytes of memory used by this object
SLONG GetUsedMemory(void)
{
// initial
SLONG slUsedMemory = sizeof(CLight) - sizeof(CRationalEntity) + CRationalEntity::GetUsedMemory();
// add some more
slUsedMemory += m_fnModel.Length();
slUsedMemory += m_fnTexture.Length();
slUsedMemory += m_fnReflection.Length();
slUsedMemory += m_fnSpecular.Length();
slUsedMemory += m_fnBump.Length();
slUsedMemory += m_strName.Length();
slUsedMemory += m_strDescription.Length();
slUsedMemory += m_fnmLightAnimation.Length();
slUsedMemory += 1* sizeof(CAnimObject); // only 1
return slUsedMemory;
}
procedures:
Die()
{
// for each child of this entity
{FOREACHINLIST(CEntity, en_lnInParent, en_lhChildren, itenChild) {
// send it destruction event
itenChild->SendEvent(ERangeModelDestruction());
}}
// spawn debris
CModelDestruction *pmd=GetDestruction();
pmd->SpawnDebris(this);
// if there is another phase in destruction
CModelHolder2 *penNext = pmd->GetNextPhase();
if (penNext!=NULL) {
// copy it here
CEntity *penNew = GetWorld()->CopyEntityInWorld( *penNext, GetPlacement() );
penNew->GetModelObject()->StretchModel(GetModelObject()->mo_Stretch);
penNew->ModelChangeNotify();
((CModelHolder2 *)penNew)->m_colBurning=m_colBurning;
((CModelHolder2 *)penNew)->m_fChainSawCutDamage=m_fChainSawCutDamage;
if( pmd->m_iStartAnim!=-1)
{
penNew->GetModelObject()->PlayAnim(pmd->m_iStartAnim, 0);
}
// copy custom shading parameters
CModelHolder2 &mhNew=*((CModelHolder2 *)penNew);
mhNew.m_cstCustomShading=m_cstCustomShading;
mhNew.m_colLight=m_colLight;
mhNew.m_colAmbient=m_colAmbient;
mhNew.m_fMipFadeDist = m_fMipFadeDist;
mhNew.m_fMipFadeLen = m_fMipFadeLen;
mhNew.m_fMipAdd = m_fMipAdd;
mhNew.m_fMipMul = m_fMipMul;
// domino death for cannonball
if( /*m_dmtLastDamageType==DMT_CANNONBALL ||*/ m_dmtLastDamageType==DMT_CHAINSAW)
{
EDeath eDeath; // we don't need any extra parameters
mhNew.m_fChainSawCutDamage=0.0f;
mhNew.m_dmtLastDamageType=DMT_CHAINSAW;
penNew->SendEvent(eDeath);
}
}
/* currently, environment destruction does not yield score.
update statistics, if score is re-enabled!
// send score to who killed you
if (m_penLastDamager!=NULL) {
EReceiveScore eScore;
eScore.fPoints = 10.0f;
m_penLastDamager->SendEvent(eScore);
}*/
// if there is a destruction target
if (m_penDestroyTarget!=NULL) {
// notify it
SendToTarget(m_penDestroyTarget, EET_TRIGGER, m_penLastDamager);
}
// destroy yourself
Destroy();
return;
}
Main()
{
// initialize the model
InitModelHolder();
// TODO: decomment this AFTER mip factors conversion
if (m_fMipFadeLenMetric>m_rMipFadeDistMetric) { m_fMipFadeLenMetric = m_rMipFadeDistMetric; }
// TODO: decomment this for mip factors conversion
/*if (m_fMipFadeLen<0.0f) { m_fMipFadeLen = 0.0f; }
if (m_fMipFadeDist<0.0f) { m_fMipFadeDist = 0.0f; }
if (m_fMipFadeLen>m_fMipFadeDist) { m_fMipFadeLen = m_fMipFadeDist; }
// if metric mip values are not initialized, get values from old mip factors
if ( m_fMipFadeDist>0.0f ) {
CModelObject *pmo = GetModelObject();
if (pmo!=NULL) {
FLOAT fMipSizeFact = Log2( Max(pmo->mo_Stretch(1),Max(pmo->mo_Stretch(2),pmo->mo_Stretch(3))));
m_rMipFadeDistMetric = pow(2.0f, m_fMipFadeDist+fMipSizeFact)/(1024.0f*MIPRATIO);
m_fMipFadeLenMetric = m_rMipFadeDistMetric - pow(2.0f, m_fMipFadeDist+fMipSizeFact-m_fMipFadeLen)/(1024.0f*MIPRATIO);
} else {
m_rMipFadeDistMetric = 0.0f;
m_fMipFadeLenMetric = 0.0f;
}
} else {
m_rMipFadeDistMetric = 0.0f;
m_fMipFadeLenMetric = 0.0f;
}*/
// convert metric factors to mip factors
if (m_rMipFadeDistMetric>0.0f) {
m_fMipFadeDist = Log2(m_rMipFadeDistMetric*1024.0f*MIPRATIO);
m_fMipFadeLen = Log2((m_rMipFadeDistMetric+m_fMipFadeLenMetric)*1024.0f*MIPRATIO) - m_fMipFadeDist;
} else {
m_fMipFadeDist = 0.0f;
m_fMipFadeLen = 0.0f;
}
// check your destruction pointer
if (m_penDestruction!=NULL && !IsOfClass(m_penDestruction, "ModelDestruction")) {
WarningMessage("Destruction '%s' is wrong class!", m_penDestruction->GetName());
m_penDestruction=NULL;
}
// wait forever
wait() {
// on the beginning
on(EBegin): {
// set your health
if (m_penDestruction!=NULL) {
SetHealth(GetDestruction()->m_fHealth);
}
resume;
}
// activate/deactivate shows/hides model
on (EActivate): {
SwitchToModel();
m_bActive = TRUE;
if (m_bColliding) {
SetPhysicsFlags(EPF_MODEL_FIXED);
SetCollisionFlags(ECF_MODEL_HOLDER);
}
resume;
}
on (EDeactivate): {
SwitchToEditorModel();
SetPhysicsFlags(EPF_MODEL_IMMATERIAL);
SetCollisionFlags(ECF_IMMATERIAL);
m_bActive = FALSE;
SetPhysicsFlags(EPF_MODEL_IMMATERIAL);
SetCollisionFlags(ECF_IMMATERIAL);
resume;
}
// when your parent is destroyed
on(ERangeModelDestruction): {
// for each child of this entity
{FOREACHINLIST(CEntity, en_lnInParent, en_lhChildren, itenChild) {
// send it destruction event
itenChild->SendEvent(ERangeModelDestruction());
}}
// destroy yourself
Destroy();
resume;
}
// when dead
on(EDeath): {
if (m_penDestruction!=NULL) {
jump Die();
}
resume;
}
// when animation should be changed
on(EChangeAnim eChange): {
m_iModelAnimation = eChange.iModelAnim;
m_iTextureAnimation = eChange.iTextureAnim;
m_iLightAnimation = eChange.iLightAnim;
if (m_aoLightAnimation.GetData()!=NULL) {
m_aoLightAnimation.PlayAnim(m_iLightAnimation, eChange.bLightLoop?AOF_LOOPING:0);
}
if (GetModelObject()->GetData()!=NULL) {
GetModelObject()->PlayAnim(m_iModelAnimation, eChange.bModelLoop?AOF_LOOPING:0);
}
if (GetModelObject()->mo_toTexture.GetData()!=NULL) {
GetModelObject()->mo_toTexture.PlayAnim(m_iTextureAnimation, eChange.bTextureLoop?AOF_LOOPING:0);
}
resume;
}
otherwise(): {
resume;
}
};
}
};