hand_guards.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. ///@{ guards
  2. /**@class HandGuardBase
  3. * @brief represents guard on a transition from state to state
  4. **/
  5. class HandGuardBase
  6. {
  7. /**@fn GuardCondition
  8. * @brief enable or disable transition based on condition
  9. * @note Item is guaranteed to exist on remote client due to replication relationships
  10. * @note The guard is a boolean operation executed first and which can prevent the transition from firing by returning false
  11. * @return true if transition is allowed
  12. **/
  13. bool GuardCondition(HandEventBase e) { return true; }
  14. };
  15. class HandGuardAnd extends HandGuardBase
  16. {
  17. ref HandGuardBase m_arg0;
  18. ref HandGuardBase m_arg1;
  19. void HandGuardAnd(HandGuardBase arg0 = null, HandGuardBase arg1 = null) { m_arg0 = arg0; m_arg1 = arg1; }
  20. override bool GuardCondition(HandEventBase e)
  21. {
  22. bool result = m_arg0.GuardCondition(e) && m_arg1.GuardCondition(e);
  23. #ifdef ENABLE_LOGGING
  24. if ( LogManager.IsInventoryHFSMLogEnable() )
  25. {
  26. Debug.InventoryHFSMLog("GuardCondition result: " + result + " - " + m_arg0.Type() + " && " + m_arg1.Type(), "HandGuardAnd" , "n/a", "GuardCondition", e.m_Player.ToString() );
  27. }
  28. #endif
  29. return result;
  30. }
  31. };
  32. class HandGuardNot extends HandGuardBase
  33. {
  34. ref HandGuardBase m_arg0;
  35. void HandGuardNot(HandGuardBase arg0 = null) { m_arg0 = arg0; }
  36. override bool GuardCondition(HandEventBase e)
  37. {
  38. bool result = !m_arg0.GuardCondition(e);
  39. #ifdef ENABLE_LOGGING
  40. if ( LogManager.IsInventoryHFSMLogEnable() )
  41. {
  42. Debug.InventoryHFSMLog("GuardCondition result: " + result + " - " + m_arg0.Type(), "HandGuardNot" , "n/a", "GuardCondition", e.m_Player.ToString() );
  43. }
  44. #endif
  45. return result;
  46. }
  47. };
  48. class HandGuardOr extends HandGuardBase
  49. {
  50. ref HandGuardBase m_arg0;
  51. ref HandGuardBase m_arg1;
  52. void HandGuardOr(HandGuardBase arg0 = null, HandGuardBase arg1 = null) { m_arg0 = arg0; m_arg1 = arg1; }
  53. override bool GuardCondition(HandEventBase e)
  54. {
  55. bool result = m_arg0.GuardCondition(e) || m_arg1.GuardCondition(e);
  56. #ifdef ENABLE_LOGGING
  57. if ( LogManager.IsInventoryHFSMLogEnable() )
  58. {
  59. Debug.InventoryHFSMLog("GuardCondition result: " + result + " - " + m_arg0.Type() + " || " + m_arg1.Type(), "HandGuardOr" , "n/a", "GuardCondition", e.m_Player.ToString() );
  60. }
  61. #endif
  62. return result;
  63. }
  64. };
  65. class HandGuardHasItemInEvent extends HandGuardBase
  66. {
  67. protected Man m_Player;
  68. void HandGuardHasItemInEvent(Man p = null) { m_Player = p; }
  69. override bool GuardCondition(HandEventBase e)
  70. {
  71. EntityAI eai = e.GetSrcEntity();
  72. if (eai != NULL /* && CanTakeInHands*/)
  73. {
  74. #ifdef ENABLE_LOGGING
  75. if ( LogManager.IsInventoryHFSMLogEnable() )
  76. {
  77. Debug.InventoryHFSMLog("GuardCondition result: true - " + eai, "HandGuardHasItemInEvent" , "n/a", "GuardCondition", m_Player.ToString() );
  78. }
  79. #endif
  80. return true;
  81. }
  82. #ifdef ENABLE_LOGGING
  83. if ( LogManager.IsInventoryHFSMLogEnable() )
  84. {
  85. Debug.InventoryHFSMLog("GuardCondition result: false - " + eai, "HandGuardHasItemInEvent" , "n/a", "GuardCondition", m_Player.ToString() );
  86. }
  87. #endif
  88. return false;
  89. }
  90. };
  91. class HandGuardHasWeaponInEvent extends HandGuardHasItemInEvent
  92. {
  93. void HandGuardHasWeapoonInEvent (Man p = null) { }
  94. override bool GuardCondition(HandEventBase e)
  95. {
  96. EntityAI eai = e.GetSrcEntity();
  97. bool result = false;
  98. if (eai)
  99. {
  100. if (eai.IsWeapon())
  101. {
  102. result = true;
  103. }
  104. }
  105. #ifdef ENABLE_LOGGING
  106. if ( LogManager.IsInventoryHFSMLogEnable() )
  107. {
  108. Debug.InventoryHFSMLog("GuardCondition result: " + result,"HandGuardHasWeaponInEvent", "n/a", "GuardCondition", e.m_Player.ToString() );
  109. }
  110. #endif
  111. return result;
  112. }
  113. };
  114. class HandGuardIsSameItemInHands extends HandGuardBase
  115. {
  116. protected Man m_Player;
  117. void HandGuardIsSameItemInHands(Man p = null) { m_Player = p; }
  118. override bool GuardCondition(HandEventBase e)
  119. {
  120. bool result = false;
  121. if (e.GetSrcEntity() == m_Player.GetHumanInventory().GetEntityInHands())
  122. {
  123. result = true;
  124. }
  125. #ifdef ENABLE_LOGGING
  126. if ( LogManager.IsInventoryHFSMLogEnable() )
  127. {
  128. Debug.InventoryHFSMLog("GuardCondition result: " + result + " - srcItem = " + e.GetSrcEntity() + " hnd= " + m_Player.GetHumanInventory().GetEntityInHands(), "HandGuardIsSameItemInHands" , "n/a", "GuardCondition", e.m_Player.ToString() );
  129. }
  130. #endif
  131. return result;
  132. }
  133. };
  134. class HandGuardHasDestroyedItemInHands extends HandGuardBase
  135. {
  136. protected Man m_Player;
  137. void HandGuardHasDestroyedItemInHands(Man p = null) { m_Player = p; }
  138. override bool GuardCondition(HandEventBase e)
  139. {
  140. EntityAI hnd = m_Player.GetHumanInventory().GetEntityInHands();
  141. if (e.GetSrcEntity())
  142. {
  143. if (e.GetSrcEntity() == hnd)
  144. {
  145. #ifdef ENABLE_LOGGING
  146. if ( LogManager.IsInventoryHFSMLogEnable() )
  147. {
  148. Debug.InventoryHFSMLog("GuardCondition result: true - has same entity in hands " + hnd, "HandGuardHasDestroyedItemInHands" , "n/a", "GuardCondition", m_Player.ToString() );
  149. }
  150. #endif
  151. return true;
  152. }
  153. if (hnd == null)
  154. {
  155. #ifdef ENABLE_LOGGING
  156. if ( LogManager.IsInventoryHFSMLogEnable() )
  157. {
  158. Debug.InventoryHFSMLog("GuardCondition result: true - hands already empty", "HandGuardHasDestroyedItemInHands" , "n/a", "GuardCondition", m_Player.ToString() );
  159. }
  160. #endif
  161. return true;
  162. }
  163. }
  164. else
  165. {
  166. #ifdef ENABLE_LOGGING
  167. if ( LogManager.IsInventoryHFSMLogEnable() )
  168. {
  169. Debug.InventoryHFSMLog("GuardCondition result: true - hands already empty and item destroyed", "HandGuardHasDestroyedItemInHands" , "n/a", "GuardCondition", m_Player.ToString() );
  170. }
  171. #endif
  172. return true;
  173. }
  174. #ifdef ENABLE_LOGGING
  175. if ( LogManager.IsInventoryHFSMLogEnable() )
  176. {
  177. Debug.InventoryHFSMLog("GuardCondition result: false - destroyed entity not in hands", "HandGuardHasDestroyedItemInHands" , "n/a", "GuardCondition", m_Player.ToString() );
  178. }
  179. #endif
  180. return false;
  181. }
  182. };
  183. class HandGuardHasItemInHands extends HandGuardBase
  184. {
  185. protected Man m_Player;
  186. void HandGuardHasItemInHands(Man p = null) { m_Player = p; }
  187. override bool GuardCondition(HandEventBase e)
  188. {
  189. bool result = false;
  190. if (m_Player.GetHumanInventory().GetEntityInHands())
  191. {
  192. result = true;
  193. }
  194. #ifdef ENABLE_LOGGING
  195. if ( LogManager.IsInventoryHFSMLogEnable() )
  196. {
  197. Debug.InventoryHFSMLog("GuardCondition result: " + result + " - " + m_Player.GetHumanInventory().GetEntityInHands(), "HandGuardHasItemInHands" , "n/a", "GuardCondition", m_Player.ToString() );
  198. }
  199. #endif
  200. return result;
  201. }
  202. };
  203. //! TODO(kumarjac): This guard is unused but it has a fault and doesn't conform with maximimal/minimal checks on "Juncture"/"Remote"
  204. class HandGuardHasRoomForItem extends HandGuardBase
  205. {
  206. protected Man m_Player;
  207. void HandGuardHasRoomForItem(Man p = null) { m_Player = p; }
  208. override bool GuardCondition(HandEventBase e)
  209. {
  210. if (e.GetDst() && e.GetDst().IsValid())
  211. {
  212. if ( !GetGame().IsDedicatedServer())
  213. {
  214. if (m_Player)
  215. m_Player.GetHumanInventory().ClearInventoryReservationEx(e.GetDst().GetItem(),e.GetDst());
  216. }
  217. if (!GameInventory.LocationTestAddEntity(e.GetDst(), false, true, true, true, true, false))
  218. {
  219. #ifdef ENABLE_LOGGING
  220. if ( LogManager.IsInventoryHFSMLogEnable() )
  221. {
  222. Debug.InventoryHFSMLog("GuardCondition result: false - no room at dst=" + InventoryLocation.DumpToStringNullSafe(e.GetDst()), "HandGuardHasRoomForItem" , "n/a", "GuardCondition", m_Player.ToString() );
  223. }
  224. #endif
  225. //if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[hndfsm] HandGuardHasRoomForItem - no room at dst=" + InventoryLocation.DumpToStringNullSafe(e.GetDst()));
  226. //Error("[hndfsm] HandGuardHasRoomForItem - no room at dst=" + InventoryLocation.DumpToStringNullSafe(e.GetDst()));
  227. return false;
  228. }
  229. if ( !GetGame().IsDedicatedServer())
  230. {
  231. if (m_Player)
  232. m_Player.GetHumanInventory().AddInventoryReservationEx(e.GetDst().GetItem(), e.GetDst(), GameInventory.c_InventoryReservationTimeoutShortMS);
  233. }
  234. #ifdef ENABLE_LOGGING
  235. if ( LogManager.IsInventoryHFSMLogEnable() )
  236. {
  237. Debug.InventoryHFSMLog("GuardCondition result: true", "HandGuardHasRoomForItem" , "n/a", "GuardCondition", m_Player.ToString() );
  238. }
  239. #endif
  240. return true;
  241. }
  242. #ifdef ENABLE_LOGGING
  243. if ( LogManager.IsInventoryHFSMLogEnable() )
  244. {
  245. Debug.InventoryHFSMLog("GuardCondition result: false - e.m_Dst is null", "HandGuardHasRoomForItem" , "n/a", "GuardCondition", m_Player.ToString() );
  246. }
  247. #endif
  248. return false;
  249. }
  250. };
  251. class HandGuardCanMove extends HandGuardBase
  252. {
  253. protected Man m_Player;
  254. void HandGuardCanMove(Man p = null) { m_Player = p; }
  255. override bool GuardCondition(HandEventBase e)
  256. {
  257. HandEventMoveTo es = HandEventMoveTo.Cast(e);
  258. bool result = e.m_IsJuncture || e.m_IsRemote;
  259. if (result == false)
  260. {
  261. result = GameInventory.LocationCanMoveEntity(es.GetSrc(), es.GetDst());
  262. }
  263. #ifdef ENABLE_LOGGING
  264. if ( LogManager.IsInventoryHFSMLogEnable() )
  265. {
  266. Debug.InventoryHFSMLog("GuardCondition result: " + result, "HandGuardCanMove" , "n/a", "GuardCondition", m_Player.ToString() );
  267. }
  268. #endif
  269. return result;
  270. }
  271. };
  272. class HandGuardCanSwap extends HandGuardBase
  273. {
  274. protected Man m_Player;
  275. void HandGuardCanSwap(Man p = NULL) { m_Player = p; }
  276. override bool GuardCondition(HandEventBase e)
  277. {
  278. HandEventSwap es = HandEventSwap.Cast(e);
  279. bool result = e.m_IsJuncture || e.m_IsRemote;
  280. if (result == false)
  281. {
  282. result = GameInventory.CanSwapEntitiesEx(es.GetSrc().GetItem(), es.m_Src2.GetItem());
  283. }
  284. #ifdef ENABLE_LOGGING
  285. if ( LogManager.IsInventoryHFSMLogEnable() )
  286. {
  287. Debug.InventoryHFSMLog("GuardCondition result: " + result, "HandGuardCanSwap" , "n/a", "GuardCondition", m_Player.ToString() );
  288. }
  289. #endif
  290. //if (LogManager.IsInventoryHFSMLogEnable()) hndDebugPrint("[hndfsm] HandGuardCanSwap guard - cannot swap");
  291. return result;
  292. }
  293. };
  294. class HandGuardCanForceSwap extends HandGuardBase
  295. {
  296. protected Man m_Player;
  297. void HandGuardCanForceSwap(Man p = NULL) { m_Player = p; }
  298. override bool GuardCondition(HandEventBase e)
  299. {
  300. HandEventForceSwap es = HandEventForceSwap.Cast(e);
  301. bool result = e.m_IsJuncture || e.m_IsRemote;
  302. if (result == false)
  303. {
  304. result = GameInventory.CanSwapEntitiesEx(es.GetSrc().GetItem(), es.m_Src2.GetItem());
  305. if (result == false && es.m_Dst2)
  306. {
  307. result = GameInventory.CanForceSwapEntitiesEx(es.GetSrc().GetItem(), es.m_Dst, es.m_Src2.GetItem(), es.m_Dst2);
  308. }
  309. }
  310. #ifdef ENABLE_LOGGING
  311. if ( LogManager.IsInventoryHFSMLogEnable() )
  312. {
  313. Debug.InventoryHFSMLog("GuardCondition result: " + result, "HandGuardCanForceSwap" , "n/a", "GuardCondition", m_Player.ToString() );
  314. }
  315. #endif
  316. return result;
  317. }
  318. };
  319. class HandGuardInstantForceSwap extends HandGuardBase
  320. {
  321. protected Man m_Player;
  322. void HandGuardInstantForceSwap(Man p = NULL) { m_Player = p; }
  323. override bool GuardCondition(HandEventBase e)
  324. {
  325. HandEventForceSwap es = HandEventForceSwap.Cast(e);
  326. InventoryLocation src1 = es.m_Src;
  327. InventoryLocation dst2 = es.m_Dst2;
  328. bool result = false;
  329. if (src1.GetType() == InventoryLocationType.CARGO && dst2.GetType() == InventoryLocationType.CARGO)
  330. {
  331. if (src1.GetParent() == dst2.GetParent())
  332. {
  333. result = true;
  334. }
  335. }
  336. #ifdef ENABLE_LOGGING
  337. if (LogManager.IsInventoryHFSMLogEnable())
  338. {
  339. Debug.InventoryHFSMLog("GuardCondition result: " + result, "HandGuardInstantForceSwap" , "n/a", "GuardCondition", m_Player.ToString() );
  340. }
  341. #endif
  342. return result;
  343. }
  344. };
  345. ///@} guards