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
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
class DayZCreatureAnimInterface
{
	private void DayZCreatureAnimInterface() {}
	private void ~DayZCreatureAnimInterface() {}
	
	//-----------------------------------------------------
	// Binds, returns -1 when error, otherwise if ok

	//! returns command index - 	
	proto native TAnimGraphCommand		BindCommand(string pCommandName);

	//!
	proto native TAnimGraphVariable 	BindVariableFloat(string pVariable);
	proto native TAnimGraphVariable 	BindVariableInt(string pVariable);
	proto native TAnimGraphVariable 	BindVariableBool(string pVariable);

	//!
	proto native TAnimGraphTag 			BindTag(string pTagName);

	//!
	proto native TAnimGraphEvent		BindEvent(string pEventName);
}

class DayZCreature extends EntityAI 
{
	#ifdef _DAYZ_CREATURE_DEBUG_SHADOW
	proto native void DebugSetShadow(DayZCreature creature);
	#endif
	
	proto native bool RegisterAnimationEvent(string event_name, string function_name);
	
	proto native void SetAnimationInstanceByName(string animation_instance_name, int instance_uuid, float duration);
	proto native int GetCurrentAnimationInstanceUUID();
	
	proto native DayZCreatureAnimInterface GetAnimInterface();

	
	proto native void UpdateSimulationPrecision(int simLOD);

	//---------------------------------------------------------
	// helper functions for disabling simulation upon death
	proto native void StartDeath();
	proto native void ResetDeath();

	proto native void ResetDeathCooldown();
	proto native bool IsDeathProcessed();
	proto native bool IsDeathConditionMet();
	
	//---------------------------------------------------------
	// bone transforms 

	//! returns bone index for a name (-1 if pBoneName doesn't exist)
	proto native 	int 		GetBoneIndexByName(string pBoneName);
	
	override bool IsDayZCreature()
	{
		return true;
	}
	
	override bool CanBeSkinned()
	{
		return true;
	}
	
	override bool IsIgnoredByConstruction()
	{
		return IsRuined();
	}
	
	override bool IsManagingArrows()
	{
		return true;
	}	
	
	override void AddArrow(Object arrow, int componentIndex, vector closeBonePosWS, vector closeBoneRotWS)
	{
		CachedObjectsArrays.ARRAY_STRING.Clear();
		GetActionComponentNameList(componentIndex, CachedObjectsArrays.ARRAY_STRING, "fire");
		
		int pivot = -1;
		
		
		for (int i = 0; i < CachedObjectsArrays.ARRAY_STRING.Count() && pivot == -1; i++)
		{
			pivot = GetBoneIndexByName(CachedObjectsArrays.ARRAY_STRING.Get(i));
		}
		
		vector parentTransMat[4];
		vector arrowTransMat[4];
		
		if (pivot == -1)
		{
			GetTransform(parentTransMat);
		}
		else
		{
			vector rotMatrix[3];
			Math3D.YawPitchRollMatrix(closeBoneRotWS * Math.RAD2DEG,rotMatrix);
			
			parentTransMat[0] = rotMatrix[0];
			parentTransMat[1] = rotMatrix[1];
			parentTransMat[2] = rotMatrix[2];
			parentTransMat[3] = closeBonePosWS;
		}
		
		arrow.GetTransform(arrowTransMat);
		Math3D.MatrixInvMultiply4(parentTransMat, arrowTransMat, arrowTransMat);
		// orthogonalize matrix - parent might be skewed
		Math3D.MatrixOrthogonalize4(arrowTransMat);
		arrow.SetTransform(arrowTransMat);
		
		AddChild(arrow, pivot);
	}
	
	override bool HasFixedActionTargetCursorPosition()
	{
		return true;
	}

	//-------------------------------------------------------------
	//!
	//! ModOverrides
	//! 
	// these functions are for modded overide in script command mods 

	bool	ModCommandHandlerBefore(float pDt, int pCurrentCommandID, bool pCurrentCommandFinished)
	{
		return false;
	}

	bool	ModCommandHandlerInside(float pDt, int pCurrentCommandID, bool pCurrentCommandFinished)
	{
		return false;
	}
	
	bool	ModCommandHandlerAfter(float pDt, int pCurrentCommandID, bool pCurrentCommandFinished)
	{
		return false;
	}
}

class DayZCreatureAI extends DayZCreature 
{
	proto native AIAgent GetAIAgent();

	proto native bool IsSoundInsideBuilding();
#ifdef DIAG_DEVELOPER
	proto native void DebugDisableAIControl();
	proto native void DebugRestoreAIControl();
#endif
	proto native void AddDamageSphere(string bone_name, string ammo_name, float radius, float duration, bool invertTeams);
	
	proto native DayZCreatureAIType GetCreatureAIType();
	/*!
	AIAgent initialization.
	Manual ai initialization for creatures created with CreateObject(... init_ai = false...).
	*/
	proto native void InitAIAgent(AIGroup group);
	proto native void DestroyAIAgent();
	
	int m_EffectTriggerCount;//how many effect triggers is this AI inside of(overlapping triggers)
	
	protected DayZPlayer m_CinematicPlayer;
	

	void DayZCreatureAI()
	{
		RegisterAnimEvents();
		SetFlags(EntityFlags.TOUCHTRIGGERS, false);
	}
	
	
	void IncreaseEffectTriggerCount()
	{
		m_EffectTriggerCount++;
	}
	
	void DecreaseEffectTriggerCount()
	{
		m_EffectTriggerCount--;
	}
	
	void AddDamageSphere(AnimDamageParams damage_params)
	{ 
		AddDamageSphere(damage_params.m_sBoneName, damage_params.m_sAmmoName, damage_params.m_fRadius, damage_params.m_fDuration, damage_params.m_bInvertTeams); 
	}
	
	override void EEKilled(Object killer)
	{
		super.EEKilled(killer);
		CreateComponent(COMP_TYPE_BODY_STAGING); // TO DO: This is never called on clients in multiplayer! That's why skinning doesn't work properly in MP. DAYZ-28269
	}
	
	AnimBootsType GetBootsType()
	{
		return AnimBootsType.None;
	}
	
	AbstractWave PlaySound(SoundObject so, SoundObjectBuilder sob)
	{
		if(so == NULL)
		{
			return NULL;
		}
		
		so.SetPosition(GetPosition());
		AbstractWave wave = GetGame().GetSoundScene().Play3D(so, sob);
		return wave;
	}
	
	void OnSoundEvent(int event_id, string event_user_string) 
	{
		AnimSoundEvent sound_event = GetCreatureAIType().GetSoundEvent(event_id);
		if(sound_event != NULL)
		{
			ProcessSoundEvent(sound_event);
		}
	}

	void OnSoundVoiceEvent(int event_id, string event_user_string)
	{
		AnimSoundVoiceEvent voice_event = GetCreatureAIType().GetSoundVoiceEvent(event_id);
		if(voice_event != NULL)
		{
			ProcessSoundVoiceEvent(voice_event);
		}
	}
	
	void OnStepEvent(int event_id, string event_user_string) 
	{
		AnimStepEvent step_event = GetCreatureAIType().GetStepEvent(event_id);
		if(step_event != NULL)
		{
			ProcessStepEvent(step_event);
		}
	}
	
	void OnDamageEvent(int event_id, string event_user_string)
	{
		AnimDamageEvent damage_event = GetCreatureAIType().GetDamageEvent(event_id);
		if(damage_event != NULL)
		{
			ProcessDamageEvent(damage_event);
		}
	}
	
	protected void RegisterAnimEvents()
	{
		if(!RegisterAnimationEvent("Sound", "OnSoundEvent"))
		{
			Print("Error registering anim. event (Sound)");
		}

		if(!RegisterAnimationEvent("SoundVoice", "OnSoundVoiceEvent"))
		{
			Print("Error registering anim. event (SoundVoice)");
		}

		if(!GetGame().IsDedicatedServer())
		{
			if(!RegisterAnimationEvent("Step", "OnStepEvent"))
			{
				Print("Error registering anim. event (Step)");
			}
		}
		
		if(!RegisterAnimationEvent("Damage", "OnDamageEvent"))
		{
			Print("Error registering anim. event (Damage)");
		}
	}
	
	private void ProcessSoundEvent(AnimSoundEvent sound_event)
	{
		if(!GetGame().IsDedicatedServer())
		{
			SoundObjectBuilder objectBuilder = sound_event.GetSoundBuilder();
			if(NULL != objectBuilder)
			{
				objectBuilder.AddEnvSoundVariables(GetPosition());
				SoundObject soundObject = objectBuilder.BuildSoundObject();
				PlaySound(soundObject, objectBuilder);
			}
		}
		
		if(GetGame().IsServer())
		{
			if(sound_event.m_NoiseParams != NULL)
				GetGame().GetNoiseSystem().AddNoise(this, sound_event.m_NoiseParams);
		}
	}

	private void ProcessSoundVoiceEvent(AnimSoundVoiceEvent sound_event)
	{
		if(!GetGame().IsDedicatedServer())
		{
			SoundObjectBuilder objectBuilder = sound_event.GetSoundBuilder();
			if(NULL != objectBuilder)
			{
				objectBuilder.AddEnvSoundVariables(GetPosition());
				SoundObject soundObject = objectBuilder.BuildSoundObject();
				AttenuateSoundIfNecessary(soundObject);
				PlaySound(soundObject, objectBuilder);
			}
		}
		
		if(GetGame().IsServer())
		{
			if(sound_event.m_NoiseParams != NULL)
				GetGame().GetNoiseSystem().AddNoise(this, sound_event.m_NoiseParams);
		}
	}

	private void ProcessStepEvent(AnimStepEvent step_event)
	{
		SoundObjectBuilder soundBuilder = step_event.GetSoundBuilder(GetSurfaceType().Hash());
		if(soundBuilder == NULL)
			return;
		
		soundBuilder.AddEnvSoundVariables(GetPosition());
		SoundObject soundObject = soundBuilder.BuildSoundObject();
		AttenuateSoundIfNecessary(soundObject);
		PlaySound(soundObject, soundBuilder);
		
		//TODO effects
	}
	
	private void ProcessDamageEvent(AnimDamageEvent damage_event)
	{
		AddDamageSphere(damage_event.m_DamageParams);
	}
	
	protected void AttenuateSoundIfNecessary(SoundObject soundObject)
	{
		if (GetGame().GetPlayer() != NULL && (IsSoundInsideBuilding() != GetGame().GetPlayer().IsSoundInsideBuilding() || GetGame().GetPlayer().IsCameraInsideVehicle()))
		{
			soundObject.SetKind(WaveKind.WAVEATTALWAYS);
		}
		else
		{
			soundObject.SetKind(WaveKind.WAVEEFFECTEX);
		}
	}
	
	bool ResistContaminatedEffect()
	{
		return false;
	}
	
	// ================
	// EASTER EGG
	// ================
	
	//Used for easter egg sound selection
	bool IsDanger()
	{
		return false;
	}
	
	string CaptureSound()
	{
		return "";
	}
	
	string ReleaseSound()
	{
		return "";
	}
	
	// ================
	// CINEMATIC CONTROLLER
	// ================
	
	void CinematicTakeControl(DayZPlayer player)
	{
		m_CinematicPlayer = player;
	}

	bool CinematicCanJump()
	{
		return true;
	}
	
	override bool ModCommandHandlerBefore(float pDt, int pCurrentCommandID, bool pCurrentCommandFinished)
	{
		if (!m_CinematicPlayer)
		{
			return super.ModCommandHandlerBefore(pDt, pCurrentCommandID, pCurrentCommandFinished);
		}
		
		UAInterface input = m_CinematicPlayer.GetInputInterface();
			
		DayZCreatureAIInputController controller;
		GetGame().GameScript.CallFunction(this, "GetInputController", controller, 0);
			
		if (!input || !controller)
		{
			return super.ModCommandHandlerBefore(pDt, pCurrentCommandID, pCurrentCommandFinished);
		}
		
		float movementX = input.SyncedValue_ID(UAAimRight) - input.SyncedValue_ID(UAAimLeft);
		
		float maxTurnSpeed = 100.0;
		movementX = Math.Clamp(movementX * maxTurnSpeed * pDt, -180, 180);
		
		if (input.SyncedValue_ID(UALookAround) > 0)
		{
			movementX = 0;
		}
		
		bool isJump = input.SyncedValue_ID(UAGetOver) > 0;
		bool isMove = input.SyncedValue_ID(UAMoveForward) > 0;
		
		bool isRest = input.SyncedValue_ID(UAMoveBack) > 0;
		bool isSleep = input.SyncedValue_ID(UAReloadMagazine) > 0;

		float heading = GetOrientation()[0] + movementX;
		
		int iAlert = 0;
		float fAlert = 0;
		int iSpeed = 0;
		float fSpeed = 0;
		
		if (isMove)
		{
			iAlert = 1;
			fAlert = 0.2;
			
			bool isSprint = input.SyncedValue_ID(UATurbo) > 0;
			bool isJog = input.SyncedValue_ID(UAWalkRunTemp) > 0;
			bool isWalk = !isSprint && !isJog;
			if (isSprint)
			{
				//! sprint
				iSpeed = 3;
			}
			else if (isJog)
			{
				//! jog
				iSpeed = 2;
			}
			else if (isWalk)
			{
				//! walk
				iSpeed = 1;
			}
		}
		
		DayZAnimalInputController animalController;
		if (Class.CastTo(animalController, controller))
		{
			animalController.OverrideBehaviourSlot(true, DayZAnimalBehaviourSlot.NON_SPECIFIC_THREAT);
			animalController.OverrideBehaviourAction(true, DayZAnimalBehaviourAction.TRAVELING_INPUT);
			
			if (!isMove)
			{
				if (isRest)
				{
					iSpeed = 0;
					animalController.OverrideBehaviourAction(true, DayZAnimalBehaviourAction.IDLE1_INPUT);
				}
				
				if (isSleep)
				{
					iSpeed = 0;
					animalController.OverrideBehaviourAction(true, DayZAnimalBehaviourAction.WALKING_INPUT);
				}
			}
		}
			
		bool lowVel = GetVelocity(this).Length() < 0.5;
		if (iSpeed > 0 && lowVel)
		{
			iAlert = 4;
			fAlert = 1.0;
			
			iSpeed = 3;
		}
		
		if (animalController)
		{
			switch (iSpeed)
			{
			case 0:
				fSpeed = 0;
				break;
			case 1:
				fSpeed = 2;
				break;
			case 2:
				fSpeed = 3;
				break;
			case 3:
				fSpeed = 5;
				break;
			}
		}
		
		controller.OverrideTurnSpeed(true, Math.PI2 / pDt);
		controller.OverrideMovementSpeed(true, fSpeed);
		controller.OverrideHeading(true, heading * Math.DEG2RAD);
		controller.OverrideAlertLevel(true, true, iAlert, fAlert);
		
		if (CinematicCanJump() && isJump)
		{
			controller.OverrideJump(true, 101, 2.0);
		}

		return true;
	}
}

enum DayZAnimalConstants
{
	COMMANDID_MOVE,
	COMMANDID_JUMP,
	COMMANDID_DEATH,
	COMMANDID_HIT,
	COMMANDID_ATTACK,
	COMMANDID_SCRIPT,
};

class DayZAnimalCommandMove extends AnimCommandBase
{

}

class DayZAnimalCommandAttack extends AnimCommandBase
{

}

class DayZAnimalCommandJump extends AnimCommandBase
{

}

class DayZAnimalCommandLookAt extends AnimCommandBase
{

}

class DayZAnimalCommandBehaviourModifier extends AnimCommandBase
{

}

class DayZAnimalCommandHit extends AnimCommandBase
{

}

class DayZAnimalCommandDeath extends AnimCommandBase
{

}

class DayZAnimalCommandAnimCallback extends AnimCommandBase
{

}

/**
*\brief DayZAnimalCommandScript fully scriptable command
*	\warning NON-MANAGED, will be managed by C++ once it is sent to the CommandHandler through DayZAnimal.StartCommand_Script
*	\note So ideally, it is best to set up the DayZAnimalCommandScript, not create any instances and start it through DayZAnimal.StartCommand_ScriptInst
*			In case an instance needs to be created, it needs manual deletion if not sent to the CommandHandler
*			But deleting it while it is in the CommandHandler will cause crashes
*/
class DayZAnimalCommandScript extends AnimCommandBase
{
	//! constructor must have 1st parameter to be DayZAnimal
	//void DayZAnimalCommandScript(DayZAnimal pAnimal)

	//---------------------------------------------------------------
	// usable everywhere

	//! this terminates command script and shows CommandHandler(  ... pCurrentCommandFinished == true );
	proto native void 	SetFlagFinished(bool pFinished);

	//---------------------------------------------------------------
	// PrePhys Update 

	//! script function usable in PrePhysUpdate
	proto native 	bool	PrePhys_GetTranslation(out vector pOutTransl);		// vec3 in local space !
	proto native 	bool	PrePhys_GetRotation(out float pOutRot[4]);         	// quaternion in local space !
	proto native 	void	PrePhys_SetTranslation(vector pInTransl); 			// vec3 in local space !
	proto native 	void	PrePhys_SetRotation(float pInRot[4]);				// quaternion in local space !

	//---------------------------------------------------------------
	// PostPhys Update 

	//! override this !
	//! final adjustment of physics state (after physics was applied)
	//! returns true if command continues running / false if command should end (or you can use SetFlagFinished(true))
	bool	PostPhysUpdate(float pDt);

	//! script function usable in PostPhysUpdate
	proto native 	void	PostPhys_GetPosition(out vector pOutTransl);		//! vec3 in world space
	proto native 	void	PostPhys_GetRotation(out float pOutRot[4]);        	//! quaternion in world space
	proto native 	void	PostPhys_SetPosition(vector pInTransl);				//! vec3 in world space
	proto native 	void	PostPhys_SetRotation(float pInRot[4]);				//! quaternion in world space
	proto native 	void	PostPhys_LockRotation();							//! do not process rotations !
}

class DayZAnimal extends DayZCreatureAI
{

	//! Melee hit components (AI targeting)	
	protected ref array<ref DayZAIHitComponent> m_HitComponentsForAI;
	protected string m_DefaultHitComponent;
	protected string m_DefaultHitPositionComponent;
	protected vector m_DefaultHitPosition;

	proto native DayZAnimalInputController GetInputController();

	proto native void StartCommand_Death(int pType, int pDirection);
	proto native void StartCommand_Move();
	proto native void StartCommand_Jump();
	proto native void StartCommand_Attack();
	proto native void StartCommand_Hit(int pType, int pDirection);
	
	//! scripted commands
	proto native DayZAnimalCommandScript StartCommand_Script(DayZAnimalCommandScript pInfectedCommand);
	proto native DayZAnimalCommandScript StartCommand_ScriptInst(typename pCallbackClass);
	proto native DayZAnimalCommandScript GetCommand_Script();
	
	proto native void SignalAIAttackStarted();
	proto native void SignalAIAttackEnded();
	
	void DayZAnimal()
	{
		// testing: animals have no inventory by default
		//GetInventory().LockInventory(LOCK_FROM_SCRIPT); // Hides animals from vicinity in inventory. Remove this if wanted otherwise.
		
		m_HitComponentsForAI = new array<ref DayZAIHitComponent>;

		RegisterHitComponentsForAI();
		
		//! sets default hit position and cache it here (mainly for impact particles)
		m_DefaultHitPosition = SetDefaultHitPosition(GetDefaultHitPositionComponent());
	}
	
	override bool IsHealthVisible()
	{
		return false;
	}	
	
	override bool IsAnimal()
	{
		return true;
	}
	
	override bool IsInventoryVisible()
	{
		return false;
	}
	
	override int GetHideIconMask()
	{
		return EInventoryIconVisibility.HIDE_VICINITY;
		/*
		if (IsAlive())
		{
			return EInventoryIconVisibility.HIDE_VICINITY;
		}
		return super.GetHideIconMask();
		*/
	}
	
	void CommandHandler(float dt, int currentCommandID, bool currentCommandFinished)
	{
		DayZAnimalInputController inputController = GetInputController();
		
		//! for mods
		if( ModCommandHandlerBefore(dt, currentCommandID, currentCommandFinished) )
		{
			return;
		}

		if (HandleDeath(currentCommandID, inputController))
		{
			return;
		}
		
		if (currentCommandFinished)
		{
			if (currentCommandID == DayZAnimalConstants.COMMANDID_ATTACK)
			{
				SignalAIAttackEnded();
			}
	
			StartCommand_Move();
	
			return;
		}
		
		//! for mods
		if( ModCommandHandlerInside(dt, currentCommandID, currentCommandFinished) )
		{
			return;
		}
	
		if (HandleDamageHit(currentCommandID))
		{
			if (currentCommandID  == DayZAnimalConstants.COMMANDID_ATTACK)
			{
				SignalAIAttackEnded();
			}
			return;
		}
	
		if (currentCommandID == DayZAnimalConstants.COMMANDID_MOVE)
		{
			if (inputController.IsJump())
			{
				StartCommand_Jump();
				return;
			}
	
			if (inputController.IsAttack())
			{
				StartCommand_Attack();
				SignalAIAttackStarted();
				return;
			}
		}
		
		//!
		if( ModCommandHandlerAfter(dt, currentCommandID, currentCommandFinished) )
		{
			return;
		}
	}
	
	bool m_DamageHitToProcess = false;
	int m_DamageHitType = 0;
	int m_DamageHitDirection = 0;
	
	bool HandleDeath(int currentCommandID, DayZAnimalInputController inputController)
	{
		if (inputController.IsDead())
		{
			if (currentCommandID == DayZAnimalConstants.COMMANDID_DEATH)
			{
				return true;
			}
	
			if (m_DamageHitToProcess)
			{
				m_DamageHitToProcess = false;
				
				StartCommand_Death(m_DamageHitType, m_DamageHitDirection);
			}
			else
			{
				StartCommand_Death(0, 0);
			}
	
			return true;
		}
		
		return false;
	}
	
	bool HandleDamageHit(int currentCommandID)
	{
		if (m_DamageHitToProcess)
		{
			m_DamageHitToProcess = false;
			
			if (currentCommandID != DayZAnimalConstants.COMMANDID_HIT)
			{
				StartCommand_Hit(m_DamageHitType, m_DamageHitDirection);
			}
			return true;
		}
		return false;
	}
	
	override void EEHitBy(TotalDamageResult damageResult, int damageType, EntityAI source, int component, string dmgZone, string ammo, vector modelPos, float speedCoef)
	{
		super.EEHitBy(damageResult, damageType, source, component, dmgZone, ammo, modelPos, speedCoef);
		m_TransportHitRegistered = false;

		int transferShockToDamageCoef = g_Game.ConfigGetInt(string.Format("%1 %2 DamageApplied transferShockToDamage", CFG_AMMO, ammo));
		if (transferShockToDamageCoef == 1)
		{
			//Print("DayZAnimal | EEHitBy | nonlethal hit");
			AddHealth("", "Health", -ConvertNonlethalDamage(damageResult.GetDamage(dmgZone, "Shock"), damageType));
		}
		else
		{
			ComponentAnimalBleeding animal_bleeding = ComponentAnimalBleeding.Cast( GetComponent( COMP_TYPE_ANIMAL_BLEEDING ) );
			animal_bleeding.CreateWound( damageResult, dmgZone, ammo );
		}
		
		int type = 0;
		int direction = 0;
		if (ComputeDamageHitParams(source, dmgZone, ammo, type, direction) == true)
		{
			QueueDamageHit(type, direction);
		}
		
	}
	
	void QueueDamageHit(int type, int direction)
	{
		m_DamageHitToProcess = true;
		m_DamageHitType = type;
		m_DamageHitDirection = direction;
	}
	
	bool ComputeDamageHitParams(EntityAI source, string dmgZone, string ammo, out int type, out int direction)
	{
		type = 0; // not used right now
		
		float angleDeg = ComputeHitDirectionAngleDeg(source);
		direction = TranslateHitAngleDegToDirectionIndex(angleDeg);
		
		direction += FindComponentDirectionOffset(dmgZone);
		
		return true;	
	}
	
	float ComputeHitDirectionAngleDeg(EntityAI source)
	{
		vector targetDirection = GetDirection();
		vector toSourceDirection = (source.GetPosition() - GetPosition());

		targetDirection[1] = 0;
		toSourceDirection[1] = 0;

		targetDirection.Normalize();
		toSourceDirection.Normalize();

		float cosFi = vector.Dot(targetDirection, toSourceDirection);
		vector cross = targetDirection * toSourceDirection;

		float dirAngleDeg = Math.Acos(cosFi) * Math.RAD2DEG;
		if ( cross[1] < 0 )
			dirAngleDeg = -dirAngleDeg;
		
		return dirAngleDeg;
	}
	
	int TranslateHitAngleDegToDirectionIndex(float angleDeg)
	{
		if (angleDeg >= -20 && angleDeg <= 20) // front
		{
			return 1; 
		}
		else if (angleDeg < 0) // left
		{
			return 2;
		}
		
		return 3; // right
	}
	
	int FindComponentDirectionOffset(string component)
	{
		const int directionCount = 4;
		
		int offset = 0;
		if (component.Length() == 0)
		{
			offset = 0;
		}
		else if (component == "Zone_Head")
		{
			offset = directionCount;
		}
		else if (component == "Zone_Chest" || component == "Zone_Legs_Front" || component == "Zone_Spine_Front" || component == "Zone_Neck")
		{
			offset = 2 * directionCount;
		}
		else
		{
			offset = 3 * directionCount;
		}
		
		return offset;
	}
	
	//-------------------------------------------------------------
	//!
	//! Phx contact event
	//! 
	
	override protected void EOnContact(IEntity other, Contact extra)
	{
		if( !IsAlive() )
			return;
		
		Transport transport = Transport.Cast(other);
		if( transport )
		{
			if ( GetGame().IsServer() )
			{
				RegisterTransportHit(transport);
			}			
		}
	}

	//! register hit components for AI melee (used by attacking AI)
	void RegisterHitComponentsForAI()
	{
		//! registers default hit compoent for the entity
		m_DefaultHitComponent = "Zone_Chest";
		//! registers default hit position component for entity
		m_DefaultHitPositionComponent = "Pelvis";

		//! register hit components that are selected by probability
		DayZAIHitComponentHelpers.RegisterHitComponent(m_HitComponentsForAI, "Zone_Chest", 50);
	}
	
	override string GetHitComponentForAI()
	{
		string hitComp;
		
		if (DayZAIHitComponentHelpers.SelectMostProbableHitComponent(m_HitComponentsForAI, hitComp))
		{
			return hitComp;
		}	
		
		return GetDefaultHitComponent();
	}
	
	override string GetDefaultHitComponent()
	{
		return m_DefaultHitComponent;
	}
	
	override string GetDefaultHitPositionComponent()
	{
		return m_DefaultHitPositionComponent;
	}

	override vector GetDefaultHitPosition()
	{
		return m_DefaultHitPosition;
	}

	protected vector SetDefaultHitPosition(string pSelection)
	{
		return GetSelectionPositionMS(pSelection);
	}
	
	override protected float ConvertNonlethalDamage(float damage, DamageType damageType)
	{
		switch (damageType)
		{
			case DamageType.CLOSE_COMBAT:
				return damage * GameConstants.NL_DAMAGE_CLOSECOMBAT_CONVERSION_ANIMALS;
			case DamageType.FIRE_ARM:
				return damage * GameConstants.NL_DAMAGE_FIREARM_CONVERSION_ANIMALS;
		}
		
		return super.ConvertNonlethalDamage(damage, damageType);
	}
}