weapondetachingmag.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // detach magazine composite state
  2. class WeaponDetachingMag_1 extends WeaponStartAction
  3. { };
  4. class WeaponDetachingMag_Store extends WeaponStateBase
  5. {
  6. Magazine m_magazine; /// magazine that will be detached
  7. ref InventoryLocation m_dst;
  8. override void OnEntry (WeaponEventBase e)
  9. {
  10. //if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, Detaching mag=" + m_magazine.ToString() + "to loc=" + InventoryLocation.DumpToStringNullSafe(m_dst)); }
  11. super.OnEntry(e);
  12. if (e)
  13. {
  14. if (!m_magazine || !m_dst)
  15. {
  16. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, error - no magazine to load from (m_magazine=NULL)");
  17. }
  18. }
  19. }
  20. override void OnAbort (WeaponEventBase e)
  21. {
  22. m_magazine = NULL;
  23. m_dst = NULL;
  24. super.OnAbort(e);
  25. }
  26. override void OnExit (WeaponEventBase e)
  27. {
  28. InventoryLocation il = new InventoryLocation;
  29. if (m_magazine.GetInventory().GetCurrentInventoryLocation(il))
  30. {
  31. if (GameInventory.LocationSyncMoveEntity(il, m_dst))
  32. {
  33. m_weapon.HideMagazine();
  34. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, ok - magazine removed from inv (inv->dst)"); }
  35. }
  36. else
  37. {
  38. // @TODO: drop on gnd
  39. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, error - cannot store detached magazine!");
  40. }
  41. }
  42. else
  43. {
  44. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store, error - cannot get curr location");
  45. }
  46. m_magazine = NULL;
  47. m_dst = NULL;
  48. super.OnExit(e);
  49. }
  50. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  51. {
  52. if (!super.SaveCurrentFSMState(ctx))
  53. return false;
  54. if (!ctx.Write(m_magazine))
  55. {
  56. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store.SaveCurrentFSMState: cannot write m_magazine for weapon=" + m_weapon);
  57. return false;
  58. }
  59. if (!OptionalLocationWriteToContext(m_dst, ctx))
  60. {
  61. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store.SaveCurrentFSMState: cannot write m_st for weapon=" + m_weapon);
  62. return false;
  63. }
  64. return true;
  65. }
  66. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  67. {
  68. if (!super.LoadCurrentFSMState(ctx, version))
  69. return false;
  70. if (!ctx.Read(m_magazine))
  71. {
  72. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store.LoadCurrentFSMState: cannot read m_magazine for weapon=" + m_weapon);
  73. return false;
  74. }
  75. if (!OptionalLocationReadFromContext(m_dst, ctx))
  76. {
  77. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag_Store.LoadCurrentFSMState: cannot read m_dst for weapon=" + m_weapon);
  78. return false;
  79. }
  80. return true;
  81. }
  82. };
  83. /*class WeaponDetachingMag_Store_W4T extends WeaponDetachingMag_Store
  84. {
  85. override bool IsWaitingForActionFinish () { return true; }
  86. };*/
  87. class WeaponDetachingMag extends WeaponStateBase
  88. {
  89. WeaponActions m_action;
  90. int m_actionType;
  91. Magazine m_magazine; /// detached magazine
  92. ref InventoryLocation m_dst; /// detached magazine destination
  93. ref WeaponStateBase m_start;
  94. ref WeaponDetachingMag_Store m_store;
  95. ref MagazineHide_W4T m_hideM;
  96. void WeaponDetachingMag (Weapon_Base w = NULL, WeaponStateBase parent = NULL, WeaponActions action = WeaponActions.NONE, int actionType = -1)
  97. {
  98. m_action = action;
  99. m_actionType = actionType;
  100. // setup nested state machine
  101. m_start = new WeaponDetachingMag_1(m_weapon, this, m_action, m_actionType);
  102. m_store = new WeaponDetachingMag_Store(m_weapon, this);
  103. m_hideM = new MagazineHide_W4T(m_weapon, this);
  104. // events
  105. WeaponEventBase _fin_ = new WeaponEventHumanCommandActionFinished;
  106. WeaponEventBase __md_ = new WeaponEventAnimMagazineDetached;
  107. WeaponEventBase __mh_ = new WeaponEventAnimMagazineHide;
  108. m_fsm = new WeaponFSM(this); // @NOTE: set owner of the submachine fsm
  109. m_fsm.AddTransition(new WeaponTransition(m_start, __md_, m_store));
  110. m_fsm.AddTransition(new WeaponTransition(m_store, __mh_, m_hideM));
  111. m_fsm.AddTransition(new WeaponTransition(m_hideM, _fin_, NULL));
  112. // Safety exits
  113. m_fsm.AddTransition(new WeaponTransition(m_store , _fin_, null));
  114. m_fsm.AddTransition(new WeaponTransition(m_start , _fin_, null));
  115. m_fsm.SetInitialState(m_start);
  116. }
  117. override void OnEntry (WeaponEventBase e)
  118. {
  119. if (e != NULL)
  120. {
  121. WeaponEventDetachMagazine de;
  122. if (Class.CastTo(de, e))
  123. {
  124. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("WeaponDetachingMag type=" + typename.EnumToString(InventoryLocationType, de.m_dst.GetType())); }
  125. m_magazine = e.m_magazine;
  126. m_dst = de.m_dst;
  127. m_store.m_magazine = m_magazine;
  128. m_store.m_dst = m_dst;
  129. if (LogManager.IsWeaponLogEnable()) { wpnDebugPrint("WeaponDetachingMag type=" + typename.EnumToString(InventoryLocationType, m_store.m_dst.GetType())); }
  130. }
  131. }
  132. super.OnEntry(e); // @NOTE: super at the end (prevent override from submachine start)
  133. }
  134. override void OnExit (WeaponEventBase e)
  135. {
  136. m_dst = NULL;
  137. m_magazine = NULL;
  138. super.OnExit(e);
  139. }
  140. override bool SaveCurrentFSMState (ParamsWriteContext ctx)
  141. {
  142. if (!super.SaveCurrentFSMState(ctx))
  143. return false;
  144. if (!ctx.Write(m_magazine))
  145. {
  146. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag.SaveCurrentFSMState: cannot write m_magazine for weapon=" + m_weapon);
  147. return false;
  148. }
  149. if (!OptionalLocationWriteToContext(m_dst, ctx))
  150. {
  151. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag.SaveCurrentFSMState: cannot write m_st for weapon=" + m_weapon);
  152. return false;
  153. }
  154. return true;
  155. }
  156. override bool LoadCurrentFSMState (ParamsReadContext ctx, int version)
  157. {
  158. if (!super.LoadCurrentFSMState(ctx, version))
  159. return false;
  160. if (!ctx.Read(m_magazine))
  161. {
  162. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag.LoadCurrentFSMState: cannot read m_magazine for weapon=" + m_weapon);
  163. return false;
  164. }
  165. if (!OptionalLocationReadFromContext(m_dst, ctx))
  166. {
  167. Error("[wpnfsm] " + Object.GetDebugName(m_weapon) + " WeaponDetachingMag.LoadCurrentFSMState: cannot read m_dst for weapon=" + m_weapon);
  168. return false;
  169. }
  170. return true;
  171. }
  172. };