/
GameRulesClientServer.cpp
1269 lines (1045 loc) · 38.7 KB
/
GameRulesClientServer.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
/*************************************************************************
Crytek Source File.
Copyright (C), Crytek Studios, 2001-2005.
-------------------------------------------------------------------------
$Id$
$DateTime$
-------------------------------------------------------------------------
History:
- 23:5:2006 9:27 : Created by Marcio Martins
*************************************************************************/
#include "StdAfx.h"
#include "ScriptBind_GameRules.h"
#include "GameRules.h"
#include "Game.h"
#include "GameCVars.h"
#include "Actor.h"
#include "Player.h"
#include "IVehicleSystem.h"
#include "IItemSystem.h"
#include "IMaterialEffects.h"
#include "WeaponSystem.h"
#include "Radio.h"
#include "Audio/GameAudio.h"
#include "Audio/SoundMoods.h"
#include "Audio/BattleStatus.h"
#include "IWorldQuery.h"
#include <StlUtils.h>
#include "HUD/UIManager.h"
#include "HUD/UIMultiPlayer.h"
#include "HitDeathReactions.h"
#include "Network/Lobby/GameLobby.h"
#include "PlayerMovementController.h"
//------------------------------------------------------------------------
void CGameRules::ClientSimpleHit(const SimpleHitInfo &simpleHitInfo)
{
if (!simpleHitInfo.remote)
{
if (!gEnv->bServer)
GetGameObject()->InvokeRMI(SvRequestSimpleHit(), simpleHitInfo, eRMI_ToServer);
else
ServerSimpleHit(simpleHitInfo);
}
}
//------------------------------------------------------------------------
void CGameRules::ClientHit(const HitInfo &hitInfo)
{
FUNCTION_PROFILER(GetISystem(), PROFILE_GAME);
IActor *pClientActor = g_pGame->GetIGameFramework()->GetClientActor();
IActor *pActor = g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(hitInfo.targetId);
if(pActor == pClientActor)
if (gEnv->pInput) gEnv->pInput->ForceFeedbackEvent( SFFOutputEvent(eDI_XI, eFF_Rumble_Basic, 0.5f * hitInfo.damage * 0.01f, hitInfo.damage * 0.02f, 0.0f));
CreateScriptHitInfo(m_scriptHitInfo, hitInfo);
CallScript(m_clientStateScript, "OnHit", m_scriptHitInfo);
bool backface = hitInfo.dir.Dot(hitInfo.normal)>0;
if (!hitInfo.remote && hitInfo.targetId && !backface)
{
if (!gEnv->bServer)
{
GetGameObject()->InvokeRMI(SvRequestHit(), hitInfo, eRMI_ToServer);
}
else
{
ServerHit(hitInfo);
}
if(gEnv->IsClient())
ProcessLocalHit(hitInfo);
}
}
//------------------------------------------------------------------------
void CGameRules::ServerSimpleHit(const SimpleHitInfo &simpleHitInfo)
{
switch (simpleHitInfo.type)
{
case 0: // tag
{
if (!simpleHitInfo.targetId)
return;
// tagged entities are temporary in MP, not in SP.
bool temp = gEnv->bMultiplayer;
AddTaggedEntity(simpleHitInfo.shooterId, simpleHitInfo.targetId, temp);
}
break;
case 1: // tac
{
if (!simpleHitInfo.targetId)
return;
CActor *pActor = (CActor *)gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor(simpleHitInfo.targetId);
if (pActor && pActor->CanSleep())
pActor->Fall(Vec3(0.0f,0.0f,0.0f),simpleHitInfo.value);
}
break;
case 0xe: // freeze
{
if (!simpleHitInfo.targetId)
return;
// call OnFreeze
bool allow=true;
if (m_serverStateScript.GetPtr() && m_serverStateScript->GetValueType("OnFreeze")==svtFunction)
{
HSCRIPTFUNCTION func=0;
m_serverStateScript->GetValue("OnFreeze", func);
Script::CallReturn(m_serverStateScript->GetScriptSystem(), func, m_script, ScriptHandle(simpleHitInfo.targetId), ScriptHandle(simpleHitInfo.shooterId), ScriptHandle(simpleHitInfo.weaponId), simpleHitInfo.value, allow);
gEnv->pScriptSystem->ReleaseFunc(func);
}
if (!allow)
return;
if (IEntity *pEntity=gEnv->pEntitySystem->GetEntity(simpleHitInfo.targetId))
{
IScriptTable *pScriptTable=pEntity->GetScriptTable();
// call OnFrost
if (pScriptTable && pScriptTable->GetValueType("OnFrost")==svtFunction)
{
HSCRIPTFUNCTION func=0;
pScriptTable->GetValue("OnFrost", func);
Script::Call(pScriptTable->GetScriptSystem(), func, pScriptTable, ScriptHandle(simpleHitInfo.shooterId), ScriptHandle(simpleHitInfo.weaponId), simpleHitInfo.value);
gEnv->pScriptSystem->ReleaseFunc(func);
}
FreezeEntity(simpleHitInfo.targetId, true, true, simpleHitInfo.value>0.999f);
}
}
break;
default:
assert(!"Unknown Simple Hit type!");
}
}
//------------------------------------------------------------------------
void CGameRules::ServerHit(const HitInfo &hitInfo)
{
if (m_processingHit)
{
m_queuedHits.push(hitInfo);
return;
}
++m_processingHit;
ProcessServerHit(hitInfo);
while (!m_queuedHits.empty())
{
HitInfo info(m_queuedHits.front());
ProcessServerHit(info);
m_queuedHits.pop();
}
--m_processingHit;
}
//------------------------------------------------------------------------
void CGameRules::ProcessServerHit(const HitInfo &hitInfo)
{
bool ok=true;
// check if shooter is alive
CActor *pShooter = GetActorByEntityId(hitInfo.shooterId);
CActor *pTarget = GetActorByEntityId(hitInfo.targetId);
if (hitInfo.shooterId)
{
if (pShooter && pShooter->GetHealth()<=0)
ok=false;
}
if (hitInfo.targetId)
{
if (pTarget && pTarget->GetSpectatorMode())
ok=false;
}
if (ok)
{
float fTargetHealthBeforeHit = 0.0f;
if(pTarget)
{
fTargetHealthBeforeHit = pTarget->GetHealth();
}
CreateScriptHitInfo(m_scriptHitInfo, hitInfo);
CallScript(m_serverStateScript, "OnHit", m_scriptHitInfo);
if(pTarget && !pTarget->IsDead())
{
const float fCausedDamage = fTargetHealthBeforeHit - pTarget->GetHealth();
ProcessLocalHit(hitInfo, fCausedDamage);
}
// call hit listeners if any
if (m_hitListeners.empty() == false)
{
for (size_t i = 0; i < m_hitListeners.size(); )
{
size_t count = m_hitListeners.size();
m_hitListeners[i]->OnHit(hitInfo);
if (count == m_hitListeners.size())
i++;
else
continue;
}
}
if (pShooter && hitInfo.shooterId!=hitInfo.targetId && hitInfo.weaponId!=hitInfo.shooterId && hitInfo.weaponId!=hitInfo.targetId && hitInfo.damage>=0)
{
EntityId params[2];
params[0] = hitInfo.weaponId;
params[1] = hitInfo.targetId;
m_pGameplayRecorder->Event(pShooter->GetEntity(), GameplayEvent(eGE_WeaponHit, 0, 0, (void *)params));
}
if (pShooter)
m_pGameplayRecorder->Event(pShooter->GetEntity(), GameplayEvent(eGE_Hit, 0, 0, (void *)hitInfo.weaponId));
if (pShooter)
m_pGameplayRecorder->Event(pShooter->GetEntity(), GameplayEvent(eGE_Damage, 0, hitInfo.damage, (void *)hitInfo.weaponId));
}
}
void CGameRules::ProcessLocalHit( const HitInfo& hitInfo, float fCausedDamage /*= 0.0f*/ )
{
//Place the code that should be invoked in both server and client sides here
IActor* pActor = g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(hitInfo.targetId);
if (pActor != NULL && (pActor->GetActorClass() == CPlayer::GetActorClassType()))
{
// Clients sometimes want to force a hit death reaction to play (when the victim isnt actually dead.. but will be when the server responds to a hit req)
CHitDeathReactionsPtr pHitDeathReactions = static_cast<CPlayer*>(pActor)->GetHitDeathReactions();
if (pHitDeathReactions)
{
// If the user has requested the player be force killed, then we need to *react* like this was a kill
if(hitInfo.forceLocalKill)
{
// Force the hit death reaction to react as if was a kill
CActor::KillParams params;
params.shooterId = hitInfo.shooterId;
params.targetId = hitInfo.targetId;
params.weaponId = hitInfo.weaponId;
params.projectileId = hitInfo.projectileId;
params.itemIdToDrop = -1;
params.weaponClassId = hitInfo.weaponClassId;
params.damage = hitInfo.damage;
params.material = -1;
params.hit_type = hitInfo.type;
params.hit_joint = hitInfo.partId;
params.projectileClassId = hitInfo.projectileClassId;
params.penetration = hitInfo.penetrationCount;
params.firstKill = false;
params.killViaProxy = hitInfo.hitViaProxy;
params.impulseScale = hitInfo.impulseScale;
params.forceLocalKill = hitInfo.forceLocalKill;
pHitDeathReactions->OnKill(params);
}
else
{
// Proceed as normal
pHitDeathReactions->OnHit(hitInfo, fCausedDamage);
}
}
}
}
//------------------------------------------------------------------------
void CGameRules::ServerExplosion(const ExplosionInfo &explosionInfo)
{
m_queuedExplosions.push(explosionInfo);
}
//------------------------------------------------------------------------
void CGameRules::ProcessServerExplosion(const ExplosionInfo &explosionInfo)
{
//CryLog("[ProcessServerExplosion] (frame %i) shooter %i, damage %.0f, radius %.1f", gEnv->pRenderer->GetFrameID(), explosionInfo.shooterId, explosionInfo.damage, explosionInfo.radius);
GetGameObject()->InvokeRMI(ClExplosion(), explosionInfo, eRMI_ToRemoteClients);
ClientExplosion(explosionInfo);
}
//------------------------------------------------------------------------
void CGameRules::ProcessQueuedExplosions()
{
const static uint8 nMaxExp = 3;
for (uint8 exp=0; !m_queuedExplosions.empty() && exp<nMaxExp; ++exp)
{
ExplosionInfo info(m_queuedExplosions.front());
ProcessServerExplosion(info);
m_queuedExplosions.pop();
}
}
//------------------------------------------------------------------------
void CGameRules::KnockActorDown( EntityId actorEntityId )
{
// Forbid fall and play if the actor is playing a hit/death reaction
CActor* pHitActor = static_cast<CActor*>(gEnv->pGame->GetIGameFramework()->GetIActorSystem()->GetActor( actorEntityId ));
if (pHitActor)
{
if (pHitActor->GetActorClass() == CPlayer::GetActorClassType())
{
// Don't trigger Fall and Play if the actor is playing a hit reaction
CPlayer* pHitPlayer = static_cast<CPlayer*>(pHitActor);
CHitDeathReactionsConstPtr pHitDeathReactions = pHitPlayer->GetHitDeathReactions();
if (!pHitDeathReactions || !pHitDeathReactions->IsInReaction())
pHitActor->Fall();
}
else
pHitActor->Fall();
}
}
//------------------------------------------------------------------------
void CGameRules::CullEntitiesInExplosion(const ExplosionInfo &explosionInfo)
{
if (!g_pGameCVars->g_ec_enable || explosionInfo.damage <= 0.1f)
return;
IPhysicalEntity **pents;
float radiusScale = g_pGameCVars->g_ec_radiusScale;
float minVolume = g_pGameCVars->g_ec_volume;
float minExtent = g_pGameCVars->g_ec_extent;
int removeThreshold = max(1, g_pGameCVars->g_ec_removeThreshold);
IActor *pClientActor = g_pGame->GetIGameFramework()->GetClientActor();
Vec3 radiusVec(radiusScale * explosionInfo.physRadius);
int i = gEnv->pPhysicalWorld->GetEntitiesInBox(explosionInfo.pos-radiusVec,explosionInfo.pos+radiusVec,pents, ent_rigid|ent_sleeping_rigid);
int removedCount = 0;
static IEntityClass* s_pInteractiveEntityClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass("InteractiveEntity");
static IEntityClass* s_pDeadBodyClass = gEnv->pEntitySystem->GetClassRegistry()->FindClass("DeadBody");
if (i > removeThreshold)
{
int entitiesToRemove = i - removeThreshold;
for(--i;i>=0;i--)
{
if(removedCount>=entitiesToRemove)
break;
IEntity * pEntity = (IEntity*) pents[i]->GetForeignData(PHYS_FOREIGN_ID_ENTITY);
if (pEntity)
{
// don't remove if entity is held by the player
if (pClientActor && pEntity->GetId()==pClientActor->GetGrabbedEntityId())
continue;
// don't remove items/pickups
if (IItem* pItem = g_pGame->GetIGameFramework()->GetIItemSystem()->GetItem(pEntity->GetId()))
{
continue;
}
// don't remove enemies/ragdolls
if (IActor* pActor = g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(pEntity->GetId()))
{
continue;
}
// if there is a flowgraph attached, never remove!
if (pEntity->GetProxy(ENTITY_PROXY_FLOWGRAPH) != 0)
continue;
IEntityClass* pClass = pEntity->GetClass();
if (pClass == s_pInteractiveEntityClass || pClass == s_pDeadBodyClass)
continue;
// get bounding box
if (IEntityPhysicalProxy* pPhysProxy = (IEntityPhysicalProxy*)pEntity->GetProxy(ENTITY_PROXY_PHYSICS))
{
AABB aabb;
pPhysProxy->GetWorldBounds(aabb);
// don't remove objects which are larger than a predefined minimum volume
if (aabb.GetVolume() > minVolume)
continue;
// don't remove objects which are larger than a predefined minimum volume
Vec3 size(aabb.GetSize().abs());
if (size.x > minExtent || size.y > minExtent || size.z > minExtent)
continue;
}
// marcok: somehow editor doesn't handle deleting non-dynamic entities very well
// but craig says, hiding is not synchronized for DX11 breakable MP, so we remove entities only when playing pure game
// alexl: in SinglePlayer, we also currently only hide the object because it could be part of flowgraph logic
// which would break if Entity was removed and could not propagate events anymore
if (gEnv->bMultiplayer == false || gEnv->IsEditor())
{
pEntity->Hide(true);
}
else
{
gEnv->pEntitySystem->RemoveEntity(pEntity->GetId());
}
removedCount++;
}
}
}
}
//------------------------------------------------------------------------
void CGameRules::ClientExplosion(const ExplosionInfo &explosionInfo)
{
// let 3D engine know about explosion (will create holes and remove vegetation)
if (explosionInfo.hole_size > 1.0f && gEnv->p3DEngine->GetIVoxTerrain())
{
gEnv->p3DEngine->OnExplosion(explosionInfo.pos, explosionInfo.hole_size, true);
}
TExplosionAffectedEntities affectedEntities;
if (gEnv->bServer)
{
CullEntitiesInExplosion(explosionInfo);
pe_explosion explosion;
explosion.epicenter = explosionInfo.pos;
explosion.rmin = explosionInfo.minRadius;
explosion.rmax = explosionInfo.radius;
if (explosion.rmax==0)
explosion.rmax=0.0001f;
explosion.r = explosion.rmin;
explosion.impulsivePressureAtR = explosionInfo.pressure;
explosion.epicenterImp = explosionInfo.pos;
explosion.explDir = explosionInfo.dir;
explosion.nGrow = 0;
explosion.rminOcc = 0.07f;
// we separate the calls to SimulateExplosion so that we can define different radii for AI and physics bodies
explosion.holeSize = 0.0f;
explosion.nOccRes = explosion.rmax>50.0f ? 0:16;
gEnv->pPhysicalWorld->SimulateExplosion( &explosion, 0, 0, ent_living);
CreateScriptExplosionInfo(m_scriptExplosionInfo, explosionInfo);
UpdateAffectedEntitiesSet(affectedEntities, &explosion);
// check vehicles
IVehicleSystem *pVehicleSystem = g_pGame->GetIGameFramework()->GetIVehicleSystem();
uint32 vcount = pVehicleSystem->GetVehicleCount();
if (vcount > 0)
{
IVehicleIteratorPtr iter = g_pGame->GetIGameFramework()->GetIVehicleSystem()->CreateVehicleIterator();
while (IVehicle* pVehicle = iter->Next())
{
if(IEntity *pEntity = pVehicle->GetEntity())
{
AABB aabb;
pEntity->GetWorldBounds(aabb);
IPhysicalEntity* pEnt = pEntity->GetPhysics();
if (pEnt && aabb.GetDistanceSqr(explosionInfo.pos) <= explosionInfo.radius*explosionInfo.radius)
{
float affected = gEnv->pPhysicalWorld->CalculateExplosionExposure(&explosion, pEnt);
AddOrUpdateAffectedEntity(affectedEntities, pEntity, affected);
}
}
}
}
explosion.rmin = explosionInfo.minPhysRadius;
explosion.rmax = explosionInfo.physRadius;
if (explosion.rmax==0)
explosion.rmax=0.0001f;
explosion.r = explosion.rmin;
explosion.holeSize = explosionInfo.hole_size;
if (explosion.nOccRes>0)
explosion.nOccRes = -1; // makes second call re-use occlusion info
gEnv->pPhysicalWorld->SimulateExplosion( &explosion, 0, 0, ent_rigid|ent_sleeping_rigid|ent_independent|ent_static | ent_delayed_deformations);
UpdateAffectedEntitiesSet(affectedEntities, &explosion);
CommitAffectedEntitiesSet(m_scriptExplosionInfo, affectedEntities);
float fSuitEnergyBeforeExplosion = 0.0f;
float fHealthBeforeExplosion = 0.0f;
IActor *pClientActor = g_pGame->GetIGameFramework()->GetClientActor();
if(pClientActor)
{
fHealthBeforeExplosion = (float)pClientActor->GetHealth();
}
CallScript(m_serverStateScript, "OnExplosion", m_scriptExplosionInfo);
if(pClientActor)
{
float fDeltaHealth = fHealthBeforeExplosion - static_cast<CPlayer *>(pClientActor)->GetHealth();
if(fDeltaHealth >= 20.0f)
{
SAFE_GAMEAUDIO_SOUNDMOODS_FUNC(AddSoundMood(SOUNDMOOD_EXPLOSION, MIN(fDeltaHealth, 100.0f) ));
}
}
// call hit listeners if any
if (m_hitListeners.empty() == false)
{
for (size_t i = 0; i < m_hitListeners.size(); )
{
size_t count = m_hitListeners.size();
m_hitListeners[i]->OnServerExplosion(explosionInfo);
if (count == m_hitListeners.size())
i++;
else
continue;
}
}
}
if (gEnv->IsClient())
{
if (explosionInfo.pParticleEffect)
explosionInfo.pParticleEffect->Spawn(true, IParticleEffect::ParticleLoc(explosionInfo.pos, explosionInfo.dir, explosionInfo.effect_scale));
if (!gEnv->bServer)
{
CreateScriptExplosionInfo(m_scriptExplosionInfo, explosionInfo);
}
else
{
affectedEntities.clear();
CommitAffectedEntitiesSet(m_scriptExplosionInfo, affectedEntities);
}
CallScript(m_clientStateScript, "OnExplosion", m_scriptExplosionInfo);
// call hit listeners if any
if (m_hitListeners.empty() == false)
{
THitListenerVec::iterator iter = m_hitListeners.begin();
while (iter != m_hitListeners.end())
{
(*iter)->OnExplosion(explosionInfo);
++iter;
}
}
}
ProcessClientExplosionScreenFX(explosionInfo);
ProcessExplosionMaterialFX(explosionInfo);
if (gEnv->pAISystem && !gEnv->bMultiplayer)
{
// Associate event with vehicle if the shooter is in a vehicle (tank cannon shot, etc)
EntityId ownerId = explosionInfo.shooterId;
IActor* pActor = g_pGame->GetIGameFramework()->GetIActorSystem()->GetActor(ownerId);
if (pActor && pActor->GetLinkedVehicle() && pActor->GetLinkedVehicle()->GetEntityId())
ownerId = pActor->GetLinkedVehicle()->GetEntityId();
if (ownerId != 0)
{
SAIStimulus stim(AISTIM_EXPLOSION, 0, ownerId, 0,
explosionInfo.pos, ZERO, explosionInfo.radius);
gEnv->pAISystem->RegisterStimulus(stim);
SAIStimulus stimSound(AISTIM_SOUND, AISOUND_EXPLOSION, ownerId, 0,
explosionInfo.pos, ZERO, explosionInfo.radius * 3.0f, AISTIMPROC_FILTER_LINK_WITH_PREVIOUS);
gEnv->pAISystem->RegisterStimulus(stimSound);
}
}
}
//-------------------------------------------
void CGameRules::ProcessClientExplosionScreenFX(const ExplosionInfo &explosionInfo)
{
IActor *pClientActor = g_pGame->GetIGameFramework()->GetClientActor();
if (pClientActor)
{
//Distance
float dist = (pClientActor->GetEntity()->GetWorldPos() - explosionInfo.pos).len();
//Is the explosion in Player's FOV (let's suppose the FOV a bit higher, like 80)
CActor *pActor = (CActor *)pClientActor;
SMovementState state;
if (IMovementController *pMV = pActor->GetMovementController())
{
pMV->GetMovementState(state);
}
Vec3 eyeToExplosion = explosionInfo.pos - state.eyePosition;
eyeToExplosion.Normalize();
bool inFOV = (state.eyeDirection.Dot(eyeToExplosion) > 0.68f);
// if in a vehicle eyeDirection is wrong
if(pActor && pActor->GetLinkedVehicle())
{
Vec3 eyeDir = static_cast<CPlayer*>(pActor)->GetVehicleViewDir();
inFOV = (eyeDir.Dot(eyeToExplosion) > 0.68f);
}
//All explosions have radial blur (default 30m radius, to make Sean happy =))
float maxBlurDistance = (explosionInfo.maxblurdistance>0.0f)?explosionInfo.maxblurdistance:30.0f;
if (maxBlurDistance>0.0f && g_pGameCVars->g_radialBlur>0.0f && m_explosionScreenFX && explosionInfo.radius>0.5f)
{
if (inFOV && dist < maxBlurDistance)
{
ray_hit hit;
int col = gEnv->pPhysicalWorld->RayWorldIntersection(explosionInfo.pos , -eyeToExplosion*dist, ent_static | ent_terrain, rwi_stop_at_pierceable|rwi_colltype_any, &hit, 1);
//If there was no obstacle between flashbang grenade and player
if(!col)
{
if (CScreenEffects* pSE = pActor->GetScreenEffects())
{
float blurRadius = (-1.0f/maxBlurDistance)*dist + 1.0f;
gEnv->p3DEngine->SetPostEffectParam("FilterRadialBlurring_Radius", blurRadius);
gEnv->p3DEngine->SetPostEffectParam("FilterRadialBlurring_Amount", 1.0f);
IBlendedEffect *pBlur = CBlendedEffect<CPostProcessEffect>::Create(CPostProcessEffect(pClientActor->GetEntityId(),"FilterRadialBlurring_Amount", 0.0f));
IBlendType *pLinear = CBlendType<CLinearBlend>::Create(CLinearBlend(1.0f));
pSE->StartBlend(pBlur, pLinear, 1.0f, CScreenEffects::eSFX_GID_RBlur);
pSE->SetUpdateCoords("FilterRadialBlurring_ScreenPosX","FilterRadialBlurring_ScreenPosY", explosionInfo.pos);
}
float distAmp = 1.0f - (dist / maxBlurDistance);
if (gEnv->pInput)
gEnv->pInput->ForceFeedbackEvent( SFFOutputEvent(eDI_XI, eFF_Rumble_Basic, 0.5f, distAmp*3.0f, 0.0f));
}
}
}
//Flashbang effect
if(dist<explosionInfo.radius && inFOV &&
(!strcmp(explosionInfo.effect_class,"flashbang") || !strcmp(explosionInfo.effect_class,"FlashbangAI")))
{
ray_hit hit;
int col = gEnv->pPhysicalWorld->RayWorldIntersection(explosionInfo.pos , -eyeToExplosion*dist, ent_static | ent_terrain, rwi_stop_at_pierceable|rwi_colltype_any, &hit, 1);
//If there was no obstacle between flashbang grenade and player
if(!col)
{
float power = explosionInfo.flashbangScale;
power *= max(0.0f, 1 - (dist/explosionInfo.radius));
float lookingAt = (eyeToExplosion.Dot(state.eyeDirection.normalize()) + 1)*0.5f;
power *= lookingAt;
SAFE_GAMEAUDIO_SOUNDMOODS_FUNC(AddSoundMood(SOUNDMOOD_EXPLOSION,MIN(power*40.0f,100.0f)));
gEnv->p3DEngine->SetPostEffectParam("Flashbang_Time", 1.0f + (power * 4));
gEnv->p3DEngine->SetPostEffectParam("FlashBang_BlindAmount",explosionInfo.blindAmount);
gEnv->p3DEngine->SetPostEffectParam("Flashbang_DifractionAmount", (power * 2));
gEnv->p3DEngine->SetPostEffectParam("Flashbang_Active", 1);
}
}
else if(inFOV && (dist < explosionInfo.radius))
{
if (explosionInfo.damage>10.0f || explosionInfo.pressure>100.0f)
{
//Add some angular impulse to the client actor depending on distance, direction...
float dt = (1.0f - dist/explosionInfo.radius);
dt = dt * dt;
float angleZ = gf_PI*0.15f*dt;
float angleX = gf_PI*0.15f*dt;
pActor->AddAngularImpulse(Ang3(Random(-angleX*0.5f,angleX),0.0f,Random(-angleZ,angleZ)),0.0f,dt*2.0f);
}
}
float fDist2=(pClientActor->GetEntity()->GetWorldPos()-explosionInfo.pos).len2();
if (fDist2<250.0f*250.0f)
{
if (fDist2<sqr(SAFE_GAMEAUDIO_BATTLESTATUS_FUNC_RET(GetBattleRange())))
SAFE_GAMEAUDIO_BATTLESTATUS_FUNC(TickBattleStatus(1.0f));
}
}
}
//---------------------------------------------------
void CGameRules::UpdateScoreBoardItem(EntityId id, const string name, int kills, int deaths)
{
NOTIFY_UI_MP( UpdateScoreBoardItem(id, name, kills, deaths) );
}
//---------------------------------------------------
void CGameRules::ProcessExplosionMaterialFX(const ExplosionInfo &explosionInfo)
{
// if an effect was specified, don't use MFX
if (explosionInfo.pParticleEffect)
return;
// impact stuff here
SMFXRunTimeEffectParams params;
params.soundSemantic = eSoundSemantic_Explosion;
params.pos = params.decalPos = explosionInfo.pos;
params.trg = 0;
params.trgRenderNode = 0;
Vec3 gravity;
pe_params_buoyancy buoyancy;
gEnv->pPhysicalWorld->CheckAreas(params.pos, gravity, &buoyancy);
// 0 for water, 1 for air
Vec3 pos=params.pos;
params.inWater = (buoyancy.waterPlane.origin.z > params.pos.z) && (gEnv->p3DEngine->GetWaterLevel(&pos)>=params.pos.z);
params.inZeroG = (gravity.len2() < 0.0001f);
params.trgSurfaceId = 0;
static const int objTypes = ent_all;
static const unsigned int flags = rwi_stop_at_pierceable|rwi_colltype_any;
ray_hit ray;
if (explosionInfo.impact)
{
params.dir[0] = explosionInfo.impact_velocity.normalized();
params.normal = explosionInfo.impact_normal;
if (gEnv->pPhysicalWorld->RayWorldIntersection(params.pos-params.dir[0]*0.0125f, params.dir[0]*0.25f, objTypes, flags, &ray, 1))
{
params.trgSurfaceId = ray.surface_idx;
if (ray.pCollider->GetiForeignData()==PHYS_FOREIGN_ID_STATIC)
params.trgRenderNode = (IRenderNode*)ray.pCollider->GetForeignData(PHYS_FOREIGN_ID_STATIC);
}
}
else
{
params.dir[0] = gravity;
params.normal = -gravity.normalized();
if (gEnv->pPhysicalWorld->RayWorldIntersection(params.pos, gravity, objTypes, flags, &ray, 1))
{
params.trgSurfaceId = ray.surface_idx;
if (ray.pCollider->GetiForeignData()==PHYS_FOREIGN_ID_STATIC)
params.trgRenderNode = (IRenderNode*)ray.pCollider->GetForeignData(PHYS_FOREIGN_ID_STATIC);
}
}
string effectClass = explosionInfo.effect_class;
if (effectClass.empty())
effectClass = "generic";
string query = effectClass + "_explode";
if(gEnv->p3DEngine->GetWaterLevel(&explosionInfo.pos)>explosionInfo.pos.z)
query = query + "_underwater";
if(IMaterialEffects* pMaterialEffects = gEnv->pGame->GetIGameFramework()->GetIMaterialEffects())
{
TMFXEffectId effectId = pMaterialEffects->GetEffectId(query.c_str(), params.trgSurfaceId);
if (effectId == InvalidEffectId)
effectId = pMaterialEffects->GetEffectId(query.c_str(), pMaterialEffects->GetDefaultSurfaceIndex());
if (effectId != InvalidEffectId)
pMaterialEffects->ExecuteEffect(effectId, params);
}
}
//------------------------------------------------------------------------
// RMI
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestRename)
{
CActor *pActor = GetActorByEntityId(params.entityId);
if (!pActor)
return true;
RenamePlayer(pActor, params.name.c_str());
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClRenameEntity)
{
IEntity *pEntity=gEnv->pEntitySystem->GetEntity(params.entityId);
if (pEntity)
{
string old=pEntity->GetName();
pEntity->SetName(params.name.c_str());
CryLogAlways("$8%s$o renamed to $8%s", old.c_str(), params.name.c_str());
NOTIFY_UI_MP( PlayerRenamed( params.entityId, params.name ) );
// if this was a remote player, check we're not spectating them.
// If we are, we need to trigger a spectator hud update for the new name
EntityId clientId = g_pGame->GetIGameFramework()->GetClientActorId();
if(gEnv->bMultiplayer && params.entityId != clientId)
{
CActor* pClientActor = static_cast<CActor *>(g_pGame->GetIGameFramework()->GetClientActor());
}
}
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestChatMessage)
{
SendChatMessage((EChatMessageType)params.type, params.sourceId, params.targetId, params.msg.c_str());
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClChatMessage)
{
OnChatMessage((EChatMessageType)params.type, params.sourceId, params.targetId, params.msg.c_str(), params.onlyTeam);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClForbiddenAreaWarning)
{
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestRadioMessage)
{
SendRadioMessage(params.sourceId,params.msg);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClRadioMessage)
{
OnRadioMessage(params.sourceId,params.msg);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestChangeTeam)
{
CActor *pActor = GetActorByEntityId(params.entityId);
if (!pActor)
return true;
ChangeTeam(pActor, params.teamId);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestSpectatorMode)
{
CActor *pActor = GetActorByEntityId(params.entityId);
if (!pActor)
return true;
ChangeSpectatorMode(pActor, params.mode, params.targetId, params.resetAll);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClSetTeam)
{
if (!params.entityId) // ignore these for now
return true;
int oldTeam = GetTeam(params.entityId);
if (oldTeam==params.teamId)
return true;
TEntityTeamIdMap::iterator it=m_entityteams.find(params.entityId);
if (it!=m_entityteams.end())
m_entityteams.erase(it);
IActor *pActor=m_pActorSystem->GetActor(params.entityId);
bool isplayer=pActor!=0;
if (isplayer && oldTeam)
{
TPlayerTeamIdMap::iterator pit=m_playerteams.find(oldTeam);
assert(pit!=m_playerteams.end());
stl::find_and_erase(pit->second, params.entityId);
}
if (params.teamId)
{
m_entityteams.insert(TEntityTeamIdMap::value_type(params.entityId, params.teamId));
if (isplayer)
{
TPlayerTeamIdMap::iterator pit=m_playerteams.find(params.teamId);
assert(pit!=m_playerteams.end());
pit->second.push_back(params.entityId);
}
}
if(isplayer)
{
ReconfigureVoiceGroups(params.entityId,oldTeam,params.teamId);
if (pActor->IsClient())
m_pRadio->SetTeam(GetTeamName(params.teamId));
}
ScriptHandle handle(params.entityId);
CallScript(m_clientStateScript, "OnSetTeam", handle, params.teamId);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClTextMessage)
{
OnTextMessage((ETextMessageType)params.type, params.msg.c_str(),
params.params[0].empty()?0:params.params[0].c_str(),
params.params[1].empty()?0:params.params[1].c_str(),
params.params[2].empty()?0:params.params[2].c_str(),
params.params[3].empty()?0:params.params[3].c_str()
);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestSimpleHit)
{
ServerSimpleHit(params);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, SvRequestHit)
{
HitInfo info(params);
info.remote=true;
ServerHit(info);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClExplosion)
{
ClientExplosion(params);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClFreezeEntity)
{
//IEntity *pEntity=gEnv->pEntitySystem->GetEntity(params.entityId);
//CryLogAlways("ClFreezeEntity: %s %s", pEntity?pEntity->GetName():"<<null>>", params.freeze?"true":"false");
FreezeEntity(params.entityId, params.freeze, 0);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClShatterEntity)
{
ShatterEntity(params.entityId, params.pos, params.impulse);
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClSetGameTime)
{
m_endTime = params.endTime;
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClSetRoundTime)
{
m_roundEndTime = params.endTime;
return true;
}
//------------------------------------------------------------------------
IMPLEMENT_RMI(CGameRules, ClSetPreRoundTime)