Dayz Explorer  1.24.157551 (v105080)
Dayz Code Explorer by Zeroy
magnum.c
Go to the documentation of this file.
1 const float MAGNUM_ROTATION_POSITION_M1 = -0.167;
2 const float MAGNUM_ROTATION_POSITION_0 = 0.0;
3 const float MAGNUM_ROTATION_POSITION_1 = 0.167;
4 const float MAGNUM_ROTATION_POSITION_2 = 0.334;
5 const float MAGNUM_ROTATION_POSITION_3 = 0.500;
6 const float MAGNUM_ROTATION_POSITION_4 = 0.668;
7 const float MAGNUM_ROTATION_POSITION_5 = 0.835;
8 const float MAGNUM_ROTATION_POSITION_6 = 1.0;
9 
10 
12 {
13  DEFAULT = 0,
14 };
15 
17 {
18  UNKNOWN = 0,
19  DEFAULT = 1,
20 }
21 
22 class Magnum_Static_State extends WeaponStableState
23 {
24  bool init = false;
25  override void OnEntry(WeaponEventBase e)
26  {
27  if (LogManager.IsWeaponLogEnable()) { wpnPrint("[wpnstate] { Magnum stable state"); }
28  super.OnEntry(e);
29  if (init)
30  {
31  Magnum_Base magnum;
32  if (CastTo(magnum, m_weapon))
33  {
34  magnum.SyncCylinderRotation();
35  //magnum.SyncSelectionStateFromState();
36  }
37  }
38  init = true;
39  }
40  override void OnExit (WeaponEventBase e) { super.OnExit(e); if (LogManager.IsWeaponLogEnable()) { wpnPrint("[wpnstate] } Magnum stable state"); } }
41  override int GetCurrentStateID () { return MagnumStableStateID.DEFAULT; }
42  override bool HasBullet() { return m_weapon.IsChamberFull(m_weapon.GetCurrentMuzzle()); }
43  override bool HasMagazine() { return false; }
44  override bool IsJammed() { return m_weapon.IsJammed(); }
45  override bool IsSingleState() { return true; }
46 };
47 
48 
49 
50 class Magnum_Base extends Weapon_Base
51 {
52  ref WeaponStableState C;
53  int m_LastMuzzleloaded;
54  int m_ActiveMuzzle;
55  const string ATT_SLOT_CYLINDER = "RevolverCylinder";
56  const string ATT_SLOT_EJECTOR = "RevolverEjector";
57 
58  override void SetActions()
59  {
60  super.SetActions();
62 
65 
66  }
67 
68  override RecoilBase SpawnRecoilObject()
69  {
70  return new MagnumRecoil(this);
71  }
72 
73  void Magnum_Base ()
74  {
75  m_LastMuzzleloaded = 0;
76  m_ActiveMuzzle = 0;
77 
78  /*m_abilities.Insert(new AbilityRecord(WeaponActions.CHAMBERING, WeaponActionTypes.CHAMBERING_STARTLOOPABLE_CLOSED));
79  m_abilities.Insert(new AbilityRecord(WeaponActions.CHAMBERING, WeaponActionTypes.CHAMBERING_ENDLOOPABLE));
80  m_abilities.Insert(new AbilityRecord(WeaponActions.MECHANISM, WeaponActionTypes.MECHANISM_CLOSED));
81  m_abilities.Insert(new AbilityRecord(WeaponActions.MECHANISM, WeaponActionTypes.MECHANISM_SPECIAL));
82  m_abilities.Insert(new AbilityRecord(WeaponActions.UNJAMMING, WeaponActionTypes.UNJAMMING_START));
83  m_abilities.Insert(new AbilityRecord(WeaponActions.UNJAMMING, WeaponActionTypes.UNJAMMING_END));
84  m_abilities.Insert(new AbilityRecord(WeaponActions.FIRE, WeaponActionTypes.FIRE_NORMAL));
85  m_abilities.Insert(new AbilityRecord(WeaponActions.FIRE, WeaponActionTypes.FIRE_COCKED));*/
86  }
87 
88  override void InitStateMachine ()
89  {
90  m_abilities.Insert(new AbilityRecord(WeaponActions.CHAMBERING, WeaponActionChamberingTypes.CHAMBERING_STARTLOOPABLE_CLOSED));
91  m_abilities.Insert(new AbilityRecord(WeaponActions.CHAMBERING, WeaponActionChamberingTypes.CHAMBERING_STARTLOOPABLE_CLOSED_KEEP));
92  m_abilities.Insert(new AbilityRecord(WeaponActions.CHAMBERING, WeaponActionChamberingTypes.CHAMBERING_ENDLOOPABLE));
93  m_abilities.Insert(new AbilityRecord(WeaponActions.MECHANISM, WeaponActionMechanismTypes.MECHANISM_CLOSED));
94  m_abilities.Insert(new AbilityRecord(WeaponActions.MECHANISM, WeaponActionMechanismTypes.MECHANISM_SPECIAL));
95  m_abilities.Insert(new AbilityRecord(WeaponActions.FIRE, WeaponActionFireTypes.FIRE_NORMAL));
96  m_abilities.Insert(new AbilityRecord(WeaponActions.FIRE, WeaponActionFireTypes.FIRE_COCKED));
97  m_abilities.Insert(new AbilityRecord(WeaponActions.FIRE, WeaponActionFireTypes.FIRE_UNCOCKED));
98 
99 
100  // setup state machine
101  // basic weapon state
102  C = new Magnum_Static_State(this, NULL, MagnumAnimState.DEFAULT);
103 
104 
105  WeaponMagnumChambering Chamber = new WeaponMagnumChambering(this, NULL, WeaponActions.CHAMBERING, WeaponActionChamberingTypes.CHAMBERING_STARTLOOPABLE_CLOSED_KEEP,WeaponActionChamberingTypes.CHAMBERING_ENDLOOPABLE);
106  WeaponMagnumChambering Chamber_E = new WeaponMagnumChambering(this, NULL, WeaponActions.CHAMBERING, WeaponActionChamberingTypes.CHAMBERING_STARTLOOPABLE_CLOSED,WeaponActionChamberingTypes.CHAMBERING_ENDLOOPABLE);
107  //WeaponMagnumChambering Chamber_CC00 = new WeaponMagnumChambering(this, NULL, WeaponActions.CHAMBERING, WeaponActionChamberingTypes.CHAMBERING_END);
108 
109  WeaponCharging Mech = new WeaponCharging(this, NULL, WeaponActions.MECHANISM, WeaponActionMechanismTypes.MECHANISM_CLOSED);
110 
111  WeaponStateBase Trigger_normal = new WeaponFireMagnum(this, NULL, WeaponActions.FIRE, WeaponActionFireTypes.FIRE_NORMAL);
112  WeaponStateBase Trigger_dry = new WeaponFireMagnum(this, NULL, WeaponActions.FIRE, WeaponActionFireTypes.FIRE_COCKED);
113 
114  // events
115 
116  WeaponEventBase __L__ = new WeaponEventLoad1Bullet;
117  WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
118  WeaponEventBase _abt_ = new WeaponEventHumanCommandActionAborted;
119  WeaponEventBase __M__ = new WeaponEventMechanism;
120  WeaponEventBase __T__ = new WeaponEventTrigger;
121 
122 
123  WeaponEventBase _rto_ = new WeaponEventReloadTimeout;
124  WeaponEventBase _dto_ = new WeaponEventDryFireTimeout;
125 
126 
127  m_fsm = new WeaponFSM();
128 
129 
130 
131  m_fsm.AddTransition(new WeaponTransition( C, __L__, Chamber, null, new WeaponGuardAnyChamberFiredOut(this))); // chamber from closed charged
132  m_fsm.AddTransition(new WeaponTransition( Chamber, _fin_, C));
133  m_fsm.AddTransition(new WeaponTransition( Chamber, _abt_, C));
134 
135 
136  m_fsm.AddTransition(new WeaponTransition( C, __L__, Chamber_E)); // chamber from closed charged
137  m_fsm.AddTransition(new WeaponTransition( Chamber_E, _fin_, C));
138  m_fsm.AddTransition(new WeaponTransition( Chamber_E, _abt_, C));
139 
140 
141  m_fsm.AddTransition(new WeaponTransition( C, __M__, Mech)); // charge from closed
142  m_fsm.AddTransition(new WeaponTransition( Mech, _fin_, C));
143  m_fsm.AddTransition(new WeaponTransition( Mech, _abt_, C));
144 
145 
146 
147  m_fsm.AddTransition(new WeaponTransition( C, __T__, Trigger_normal, null, new GuardAnd (new WeaponGuardCurrentChamberFull(this), new GuardNot(new WeaponGuardCurrentChamberFiredOut(this))) )); // fire.cocked
148  m_fsm.AddTransition(new WeaponTransition( Trigger_normal, _fin_, C));
149  m_fsm.AddTransition(new WeaponTransition( Trigger_normal, _dto_, C));
150  m_fsm.AddTransition(new WeaponTransition( Trigger_normal, _abt_, C));
151 
152  m_fsm.AddTransition(new WeaponTransition( C, __T__, Trigger_dry)); // fire.cocked
153  m_fsm.AddTransition(new WeaponTransition( Trigger_dry, _fin_, C));
154  m_fsm.AddTransition(new WeaponTransition( Trigger_dry, _dto_, C));
155  m_fsm.AddTransition(new WeaponTransition( Trigger_dry, _abt_, C));
156 
157 
158  SetInitialState(C);
159  SelectionBulletHide();
160  SetCurrentMuzzle(0);
161  //HideMagazine();
162  m_fsm.Start();
163  }
164 
165  override bool CanChamberBullet(int muzzleIndex, Magazine mag)
166  {
167  for (int i = 0; i < GetMuzzleCount(); i++)
168  {
169  if ( CanChamberFromMag(i, mag) )
170  {
171  if (IsChamberEmpty(i))
172  return true;
173 
174  if (IsChamberFiredOut(i))
175  return true;
176  }
177  }
178  return false;
179  }
180 
181  override void AssembleGun()
182  {
183  super.AssembleGun();
184 
185  if ( !FindAttachmentBySlotName(ATT_SLOT_EJECTOR) )
186  {
187  GetInventory().CreateAttachment("Magnum_Ejector");
188  }
189 
190  if ( !FindAttachmentBySlotName(ATT_SLOT_CYLINDER) )
191  {
192  GetInventory().CreateAttachment("Magnum_Cylinder");
193  }
194 
195  ForceSyncSelectionState();
196  SyncCylinderRotation();
197  }
198 
199  static float GetCylinderRotation(int muzzleIndex)
200  {
201  switch (muzzleIndex)
202  {
203  case 0:
205  case 1:
207  case 2:
209  case 3:
211  case 4:
213  case 5:
215  }
216 
217  ErrorEx(string.Format("Invalid muzzle index: %1", muzzleIndex));
218 
220  }
221 
222  void SetCylinderRotationAnimationPhase(float rot, bool reset = false)
223  {
224  Magnum_Cylinder cylinder = Magnum_Cylinder.Cast(GetAttachmentByType(Magnum_Cylinder));
225  Magnum_Ejector ejector = Magnum_Ejector.Cast(GetAttachmentByType(Magnum_Ejector));
226  if (cylinder && ejector)
227  {
228  float anim_phase = cylinder.GetAnimationPhase("Rotate_Cylinder");
229  if ( Math.AbsFloat(anim_phase - rot) > 0.1 )
230  {
231  if (reset)
232  {
233  cylinder.ResetAnimationPhase("Rotate_Cylinder", rot );
234  ejector.ResetAnimationPhase("Rotate_Ejector", rot );
235  }
236  else if (rot == MAGNUM_ROTATION_POSITION_0)
237  {
238  cylinder.ResetAnimationPhase("Rotate_Cylinder", MAGNUM_ROTATION_POSITION_M1 );
239  ejector.ResetAnimationPhase("Rotate_Ejector", MAGNUM_ROTATION_POSITION_M1 );
240  }
241 
242  cylinder.SetAnimationPhase("Rotate_Cylinder", rot );
243  ejector.SetAnimationPhase("Rotate_Ejector", rot );
244  }
245  }
246  }
247 
248  void SyncCylinderRotation(bool reset = true)
249  {
250  SetCylinderRotationAnimationPhase(GetCylinderRotation(GetCurrentMuzzle()), reset);
251  }
252 
253  override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
254  {
255  super.EEHealthLevelChanged(oldLevel,newLevel,zone);
256 
257  if (GetGame().IsClient())
258  return;
259 
260  SetAttachmentsHealth();
261  }
262 
263  //sets health of cylinder and ejector objects
264  void SetAttachmentsHealth()
265  {
266  EntityAI entity;
267  ItemBase attachment;
268  for (int i = 0; i < GetInventory().AttachmentCount(); i++)
269  {
270  entity = GetInventory().GetAttachmentFromIndex(i);
271  if ( Class.CastTo(attachment,entity) )
272  {
273  attachment.SetHealth01("","Health",GetHealth01());
274  }
275  }
276  }
277 
278  override void OnFire(int muzzle_index)
279  {
280  super.OnFire(muzzle_index);
281  Magnum_Cylinder cylinder = Magnum_Cylinder.Cast(GetAttachmentByType(Magnum_Cylinder));
282  if (cylinder)
283  {
284  string bullet_nose = "bullet_nose";
285  if (muzzle_index > 0)
286  bullet_nose = string.Format("bullet_nose_" + (muzzle_index + 1));
287  cylinder.HideSelection(bullet_nose);
288  }
289  }
290 
291  override bool IsShowingChamberedBullet()
292  {
293  return false;
294  }
295 
296  override bool CanEjectBullet()
297  {
298  for (int i = 0; i < GetMuzzleCount(); i++)
299  {
300  if (IsChamberFull(i))
301  return true;
302  }
303  return false;
304  }
305 
306  override void ShowBullet(int muzzleIndex)
307  {
308  super.ShowBullet(muzzleIndex);
309 
310  Magnum_Cylinder cylinder = Magnum_Cylinder.Cast(GetAttachmentByType(Magnum_Cylinder));
311  if (cylinder)
312  {
313  string bullet = "bullet";
314  if (muzzleIndex > 0)
315  bullet = string.Format("bullet_" + (muzzleIndex + 1));
316 
317  cylinder.ShowSelection(bullet);
318 
319  if (!IsChamberFiredOut(muzzleIndex))
320  {
321  string bullet_nose = "bullet_nose";
322  if (muzzleIndex > 0)
323  bullet_nose = string.Format("bullet_nose_" + (muzzleIndex + 1));
324  cylinder.ShowSelection(bullet_nose);
325  }
326  }
327  }
328 
329  override void HideBullet(int muzzleIndex)
330  {
331  super.HideBullet(muzzleIndex);
332 
333  Magnum_Cylinder cylinder = Magnum_Cylinder.Cast(GetAttachmentByType(Magnum_Cylinder));
334  if (cylinder)
335  {
336  string bullet = "bullet";
337  if (muzzleIndex > 0)
338  bullet = string.Format("bullet_" + (muzzleIndex + 1));
339 
340  cylinder.HideSelection(bullet);
341 
342  string bullet_nose = "bullet_nose";
343  if (muzzleIndex > 0)
344  bullet_nose = string.Format("bullet_nose_" + (muzzleIndex + 1));
345  cylinder.HideSelection(bullet_nose);
346  }
347  }
348 
349  //Debug menu Spawn Ground Special
350  override void OnDebugSpawn()
351  {
352  super.OnDebugSpawn();
353 
354  EntityAI entity;
355  if ( Class.CastTo(entity, this) )
356  {
357  entity.SpawnEntityOnGroundPos("Ammo_357", entity.GetPosition());
358  }
359  }
360 };
361 
362 class Magnum extends Magnum_Base
363 {
364  override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
365  {
366  super.GetDebugActions(outputList);
367 
368  outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SEPARATOR, "", FadeColors.LIGHT_GREY));
369  outputList.Insert(new TSelectableActionInfoWithColor(SAT_DEBUG_ACTION, EActions.SPIN, "Spin", FadeColors.LIGHT_GREY));
370  }
371 
372  override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
373  {
374  if (GetGame().IsServer())
375  {
376  if (action_id == EActions.SPIN)
377  {
378  const float animPhaseOffset = 0.167;
379 
380  Magnum_Cylinder cylinder = Magnum_Cylinder.Cast(GetAttachmentByType(Magnum_Cylinder));
381  Magnum_Ejector ejector = Magnum_Ejector.Cast(GetAttachmentByType(Magnum_Ejector));
382 
383  if (cylinder)
384  {
385  float animPhase = cylinder.GetAnimationPhase("Rotate_Cylinder");
386  if (animPhase + animPhaseOffset > 1.0)
387  {
388  animPhase -= 1.0;
389  cylinder.ResetAnimationPhase("Rotate_Cylinder", animPhase);
390  ejector.ResetAnimationPhase("Rotate_Ejector", animPhase);
391  }
392 
393  cylinder.SetAnimationPhase("Rotate_Cylinder", animPhase);
394  ejector.ResetAnimationPhase("Rotate_Ejector", animPhase);
395  }
396  }
397  }
398 
399  return super.OnAction(action_id, player, ctx);
400  }
401 }
402 
403 class SawedoffMagnum extends Magnum_Base {};
404 class Magnum_Cylinder extends DummyItem {};
405 class Magnum_Ejector extends DummyItem {};
ItemBase
Definition: inventoryitem.c:730
GetGame
proto native CGame GetGame()
MagnumRecoil
Definition: magnumrecoil.c:1
IsSingleState
override bool IsSingleState()
Definition: magnum.c:45
Magnum_Base
Definition: magnum.c:362
LogManager
Definition: debug.c:734
MAGNUM_ROTATION_POSITION_0
const float MAGNUM_ROTATION_POSITION_0
Definition: magnum.c:2
MAGNUM_ROTATION_POSITION_5
const float MAGNUM_ROTATION_POSITION_5
Definition: magnum.c:7
FirearmActionLoadMultiBullet
Definition: firearmactionloadmultibullet.c:1
IsChamberFull
bool IsChamberFull(int idx)
Definition: weaponstablestate.c:154
FirearmActionLoadBulletQuick
FirearmActionLoadMultiBullet FirearmActionLoadBulletQuick
RemoveAction
void RemoveAction(typename actionName)
Definition: advancedcommunication.c:118
FirearmActionLoadMultiBulletQuick
Definition: firearmactionloadmultibullet.c:94
IsChamberFiredOut
bool IsChamberFiredOut(int idx)
Definition: weaponstablestate.c:153
AssembleGun
override void AssembleGun()
Definition: aug.c:68
WeaponActionFireTypes
WeaponActionFireTypes
Definition: human.c:912
MAGNUM_ROTATION_POSITION_2
const float MAGNUM_ROTATION_POSITION_2
Definition: magnum.c:4
GetCurrentStateID
override int GetCurrentStateID()
Definition: magnum.c:41
MAGNUM_ROTATION_POSITION_4
const float MAGNUM_ROTATION_POSITION_4
Definition: magnum.c:6
ErrorEx
enum ShapeType ErrorEx
Magnum_Base
class Magnum extends Magnum_Base Magnum_Base
WeaponActions
WeaponActions
actions
Definition: human.c:808
Serializer
Serialization general interface. Serializer API works with:
Definition: serializer.c:55
HasBullet
override bool HasBullet()
Definition: magnum.c:42
MagnumStableStateID
MagnumStableStateID
Definition: magnum.c:16
OnDebugSpawn
class Hatchback_02_Blue extends Hatchback_02 OnDebugSpawn
Definition: hatchback_02.c:404
RecoilBase
Definition: recoilbase.c:1
AddAction
void AddAction(typename actionName)
Definition: advancedcommunication.c:86
DummyItem
Definition: magnum.c:404
SetActions
void SetActions()
Definition: advancedcommunication.c:79
GetDebugActions
override void GetDebugActions(out TSelectableActionInfoArrayEx outputList)
Definition: edible_base.c:744
WeaponCharging
charging of weapon without ammo to be chambered
WeaponTransition
enum FSMTransition WeaponTransition
MAGNUM_ROTATION_POSITION_6
const float MAGNUM_ROTATION_POSITION_6
Definition: magnum.c:8
OnEntry
override void OnEntry(WeaponEventBase e)
Definition: magnum.c:25
EActions
EActions
Definition: eactions.c:1
OnAction
override bool OnAction(int action_id, Man player, ParamsReadContext ctx)
Definition: edible_base.c:755
IsJammed
override bool IsJammed()
Definition: magnum.c:44
AbilityRecord
pair ( action, actionType )
Definition: weapon_base.c:4
OnFire
void OnFire(Entity flare)
Definition: flaresimulation.c:52
init
enum MagnumStableStateID init
EEHealthLevelChanged
override void EEHealthLevelChanged(int oldLevel, int newLevel, string zone)
Definition: basebuildingbase.c:463
MAGNUM_ROTATION_POSITION_3
const float MAGNUM_ROTATION_POSITION_3
Definition: magnum.c:5
WeaponEventBase
signalize mechanism manipulation
Definition: events.c:34
WeaponStateBase
represent weapon state base
Definition: bullethide.c:1
HasMagazine
override bool HasMagazine()
Definition: magnum.c:43
m_weapon
class WeaponGuardIsDestroyed extends WeaponGuardBase m_weapon
Definition: guards.c:583
Weapon_Base
shorthand
Definition: boltactionrifle_base.c:5
wpnPrint
void wpnPrint(string s)
Definition: debug.c:1
MagnumAnimState
MagnumAnimState
Definition: magnum.c:11
OnExit
override void OnExit(WeaponEventBase e)
Definition: magnum.c:40
SAT_DEBUG_ACTION
const int SAT_DEBUG_ACTION
Definition: constants.c:424
MAGNUM_ROTATION_POSITION_M1
const float MAGNUM_ROTATION_POSITION_M1
Definition: magnum.c:1
UNKNOWN
@ UNKNOWN
Definition: magnum.c:18
WeaponActionChamberingTypes
WeaponActionChamberingTypes
Definition: human.c:868
DEFAULT
@ DEFAULT
default weapon state
Definition: magnum.c:13
WeaponFSM
weapon finite state machine
Math
Definition: enmath.c:6
Class
Super root of all classes in Enforce script.
Definition: enscript.c:10
MAGNUM_ROTATION_POSITION_1
const float MAGNUM_ROTATION_POSITION_1
Definition: magnum.c:3
TSelectableActionInfoWithColor
Param4< int, int, string, int > TSelectableActionInfoWithColor
Definition: entityai.c:97
EntityAI
Definition: building.c:5
WeaponStableState
represents weapon's stable state (i.e. the basic states that the weapon will spend the most time in)
Definition: crossbow.c:26